diff --git a/internal/clients/rorapiclient/old.go b/internal/clients/rorapiclient/old.go index 01addff..6978233 100644 --- a/internal/clients/rorapiclient/old.go +++ b/internal/clients/rorapiclient/old.go @@ -10,23 +10,8 @@ import ( "github.com/spf13/viper" ) -var rorclient *resty.Client var rorclientnonauth *resty.Client -// Deprecated: GetOrCreateRorRestyClient is deprecated. Use rorclient instead -func GetOrCreateRorRestyClient() (*resty.Client, error) { - if rorclient != nil { - return rorclient, nil - } - - rorclient = resty.New() - rorclient.SetBaseURL(viper.GetString(configconsts.API_ENDPOINT)) - rorclient.Header.Add("X-API-KEY", viper.GetString(configconsts.API_KEY)) - rorclient.Header.Set("User-Agent", fmt.Sprintf("ROR-Agent/%s", viper.GetString(configconsts.VERSION))) - - return rorclient, nil -} - // Deprecated: GetOrCreateRorRestyClientNonAuth is deprecated. Use rorclient instead func GetOrCreateRorRestyClientNonAuth() (*resty.Client, error) { if rorclientnonauth != nil { diff --git a/internal/scheduler/metrics.go b/internal/scheduler/metrics.go index 235a734..b83f327 100644 --- a/internal/scheduler/metrics.go +++ b/internal/scheduler/metrics.go @@ -142,60 +142,3 @@ func CreateNodeMetrics(node apicontracts.NodeMetricsListItem) (apicontracts.Node } return nodeMetric, nil } -func CreatePodMetricsList(k8sClient *kubernetes.Clientset) ([]apicontracts.PodMetric, error) { - var podMetricsList apicontracts.PodMetricsList - var metricsReportPods []apicontracts.PodMetric - - data, err := k8sClient.RESTClient().Get().AbsPath("apis/metrics.k8s.io/v1beta1/pods").DoRaw(context.TODO()) - if err != nil { - rlog.Error("error unmarshaling podmetrics", err) - return metricsReportPods, err - } - - err = json.Unmarshal(data, &podMetricsList) - if err != nil { - rlog.Error("error unmarshaling podmetrics", err) - return metricsReportPods, err - } - - for _, pod := range podMetricsList.Items { - metricsReportPod, err := CreatePodMetrics(pod) - if err != nil { - rlog.Error("error converting podmetrics", err) - return metricsReportPods, err - } - metricsReportPods = append(metricsReportPods, metricsReportPod) - } - return metricsReportPods, nil -} - -func CreatePodMetrics(pod apicontracts.PodMetricsListItem) (apicontracts.PodMetric, error) { - var podMetric apicontracts.PodMetric - var timestamp time.Time = pod.Timestamp - var podCpuSum int64 = 0 - var podMemorySum int64 = 0 - - for _, container := range pod.Containers { - podCpu, err := apimachinery.ParseQuantity(container.Usage.CPU) - if err != nil { - rlog.Error("error converting podmetrics", err) - return podMetric, err - } - podCpuSum = podCpuSum + podCpu.MilliValue() - podMemoryObj, err := apimachinery.ParseQuantity(container.Usage.Memory) - if err != nil { - rlog.Error("error converting podmetrics", err) - return podMetric, err - } - podMemory, _ := podMemoryObj.AsInt64() - podMemorySum = podMemorySum + podMemory - } - podMetric = apicontracts.PodMetric{ - Name: pod.Metadata.Name, - Namespace: pod.Metadata.Namespace, - TimeStamp: timestamp, - CpuUsage: podCpuSum, - MemoryUsage: podMemorySum, - } - return podMetric, nil -} diff --git a/v2/go.mod b/v2/go.mod index 17c3b9e..9b09a0e 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -6,9 +6,7 @@ require ( github.com/NorskHelsenett/ror v0.3.11-rc3 github.com/go-co-op/gocron v1.37.0 github.com/go-resty/resty/v2 v2.16.0 - github.com/google/go-cmp v0.6.0 github.com/spf13/viper v1.19.0 - github.com/stretchr/testify v1.9.0 go.uber.org/automaxprocs v1.6.0 k8s.io/apimachinery v0.31.2 k8s.io/client-go v0.31.2 @@ -37,6 +35,7 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/gnostic-models v0.6.8 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -54,7 +53,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect - github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/v2/internal/scheduler/metrics.go b/v2/internal/scheduler/metrics.go index 94db1ad..404bed8 100644 --- a/v2/internal/scheduler/metrics.go +++ b/v2/internal/scheduler/metrics.go @@ -140,60 +140,3 @@ func CreateNodeMetrics(node apicontracts.NodeMetricsListItem) (apicontracts.Node } return nodeMetric, nil } -func CreatePodMetricsList(k8sClient *kubernetes.Clientset) ([]apicontracts.PodMetric, error) { - var podMetricsList apicontracts.PodMetricsList - var metricsReportPods []apicontracts.PodMetric - - data, err := k8sClient.RESTClient().Get().AbsPath("apis/metrics.k8s.io/v1beta1/pods").DoRaw(context.TODO()) - if err != nil { - rlog.Error("error unmarshaling podmetrics", err) - return metricsReportPods, err - } - - err = json.Unmarshal(data, &podMetricsList) - if err != nil { - rlog.Error("error unmarshaling podmetrics", err) - return metricsReportPods, err - } - - for _, pod := range podMetricsList.Items { - metricsReportPod, err := CreatePodMetrics(pod) - if err != nil { - rlog.Error("error converting podmetrics", err) - return metricsReportPods, err - } - metricsReportPods = append(metricsReportPods, metricsReportPod) - } - return metricsReportPods, nil -} - -func CreatePodMetrics(pod apicontracts.PodMetricsListItem) (apicontracts.PodMetric, error) { - var podMetric apicontracts.PodMetric - var timestamp time.Time = pod.Timestamp - var podCpuSum int64 = 0 - var podMemorySum int64 = 0 - - for _, container := range pod.Containers { - podCpu, err := apimachinery.ParseQuantity(container.Usage.CPU) - if err != nil { - rlog.Error("error converting podmetrics", err) - return podMetric, err - } - podCpuSum = podCpuSum + podCpu.MilliValue() - podMemoryObj, err := apimachinery.ParseQuantity(container.Usage.Memory) - if err != nil { - rlog.Error("error converting podmetrics", err) - return podMetric, err - } - podMemory, _ := podMemoryObj.AsInt64() - podMemorySum = podMemorySum + podMemory - } - podMetric = apicontracts.PodMetric{ - Name: pod.Metadata.Name, - Namespace: pod.Metadata.Namespace, - TimeStamp: timestamp, - CpuUsage: podCpuSum, - MemoryUsage: podMemorySum, - } - return podMetric, nil -} diff --git a/v2/internal/services/authservice/authservice.go b/v2/internal/services/authservice/authservice.go deleted file mode 100644 index 725e69e..0000000 --- a/v2/internal/services/authservice/authservice.go +++ /dev/null @@ -1,31 +0,0 @@ -// TODO: Remove this file once the rorconfig is implemented -// authservice implements authorization helpers for the agent -package authservice - -import ( - "github.com/NorskHelsenett/ror/pkg/config/configconsts" - "github.com/NorskHelsenett/ror/pkg/rorresources/rortypes" - - aclmodels "github.com/NorskHelsenett/ror/pkg/models/acl" - - "github.com/NorskHelsenett/ror/pkg/apicontracts/apiresourcecontracts" - - "github.com/spf13/viper" -) - -// creaters a ownerref object for the agent -func CreateOwnerref() apiresourcecontracts.ResourceOwnerReference { - return apiresourcecontracts.ResourceOwnerReference{ - Scope: aclmodels.Acl2ScopeCluster, - Subject: viper.GetString(configconsts.CLUSTER_ID), - } - -} - -func CreateRorOwnerref() rortypes.RorResourceOwnerReference { - return rortypes.RorResourceOwnerReference{ - Scope: aclmodels.Acl2ScopeCluster, - Subject: aclmodels.Acl2Subject(viper.GetString(configconsts.CLUSTER_ID)), - } - -} diff --git a/v2/internal/services/initial_service.go b/v2/internal/services/initial_service.go deleted file mode 100644 index 7c7bfb3..0000000 --- a/v2/internal/services/initial_service.go +++ /dev/null @@ -1,52 +0,0 @@ -package services - -import ( - "fmt" - "io" - "net" - "net/http" - "strings" - - "github.com/NorskHelsenett/ror/pkg/rlog" -) - -var EgressIp string - -func GetEgressIp() { - internettCheck := "https://api.ipify.org/" - nhnCheck := "ip.nhn.no" - _, err := net.LookupIP(nhnCheck) - var apiHost string - if err != nil { - apiHost = internettCheck - } else { - apiHost = fmt.Sprintf("http://%s", nhnCheck) - } - - rlog.Info("Resolving ip", rlog.String("api host", apiHost)) - res, err := http.Get(apiHost) // #nosec G107 - we are not using user input - if err != nil { - // assuming retry but on internett - apiHost = internettCheck - res, err = http.Get(apiHost) // #nosec G107 - we are not using user input - if err != nil { - errorMsg := fmt.Sprintf("could not reach host %s", apiHost) - rlog.Info(errorMsg) - return - } - } - - body, err := io.ReadAll(res.Body) - _ = res.Body.Close() - if res.StatusCode > 299 { - rlog.Info("response failed", rlog.Int("status code", res.StatusCode), rlog.ByteString("body", body)) - return - } - - if err != nil { - rlog.Error("could not parse body", err) - return - } - - EgressIp = strings.Replace(string(body), "\n", "", -1) -} diff --git a/v2/internal/services/resourceupdatev2/client.go b/v2/internal/services/resourceupdatev2/client.go deleted file mode 100644 index 0d524d9..0000000 --- a/v2/internal/services/resourceupdatev2/client.go +++ /dev/null @@ -1,70 +0,0 @@ -package resourceupdatev2 - -import ( - "github.com/NorskHelsenett/ror-agent/v2/internal/agentconfig" - "github.com/NorskHelsenett/ror-agent/v2/internal/clients" - - "github.com/NorskHelsenett/ror/pkg/apicontracts/apiresourcecontracts" - - "github.com/NorskHelsenett/ror/pkg/rlog" - - "github.com/go-resty/resty/v2" -) - -// the function sends the resource to the ror api. If receiving a non 2xx statuscode it will retun an error. -func sendResourceUpdateToRor(resourceUpdate *apiresourcecontracts.ResourceUpdateModel) error { - rorClient, err := clients.GetOrCreateRorClient() - if err != nil { - rlog.Error("Could not get ror-api client", err) - agentconfig.IncreaseErrorCount() - return err - } - var url string - var response *resty.Response - - if resourceUpdate.Action == apiresourcecontracts.K8sActionAdd { - url = "/v1/resources" - response, err = rorClient.R(). - SetHeader("Content-Type", "application/json"). - SetBody(resourceUpdate). - Post(url) - - } else if resourceUpdate.Action == apiresourcecontracts.K8sActionUpdate { - url = "/v1/resources/uid/" + resourceUpdate.Uid - response, err = rorClient.R(). - SetHeader("Content-Type", "application/json"). - SetBody(resourceUpdate). - Put(url) - } else if resourceUpdate.Action == apiresourcecontracts.K8sActionDelete { - url = "/v1/resources/uid/" + resourceUpdate.Uid - response, err = rorClient.R(). - SetHeader("Content-Type", "application/json"). - SetBody(resourceUpdate). - Delete(url) - } - - if err != nil { - agentconfig.IncreaseErrorCount() - rlog.Error("could not send data to ror-api", err, - rlog.Int("error count", agentconfig.ErrorCount)) - return err - } - - if response == nil { - agentconfig.IncreaseErrorCount() - rlog.Error("response is nil", err, - rlog.Int("error count", agentconfig.ErrorCount)) - return err - } - - if !response.IsSuccess() { - agentconfig.IncreaseErrorCount() - rlog.Info("got non 200 statuscode from ror-api", rlog.Int("status code", response.StatusCode()), - rlog.Int("error count", agentconfig.ErrorCount)) - return err - } else { - agentconfig.ResetErrorCount() - rlog.Debug("partial update sent to ror", rlog.String("api verson", resourceUpdate.ApiVersion), rlog.String("kind", resourceUpdate.Kind), rlog.String("uid", resourceUpdate.Uid)) - } - return nil -} diff --git a/v2/internal/services/resourceupdatev2/hashlist.go b/v2/internal/services/resourceupdatev2/hashlist.go index 06ae82d..eff83d2 100644 --- a/v2/internal/services/resourceupdatev2/hashlist.go +++ b/v2/internal/services/resourceupdatev2/hashlist.go @@ -4,42 +4,11 @@ import ( "fmt" "github.com/NorskHelsenett/ror-agent/v2/internal/clients" - "github.com/NorskHelsenett/ror-agent/v2/internal/services/authservice" "github.com/NorskHelsenett/ror/pkg/apicontracts/v2/apicontractsv2resources" - "github.com/NorskHelsenett/ror/pkg/rlog" ) -// Hashlist for use in agent communication -type hashList struct { - Items []hashItem `json:"items"` -} - -// Item for use in the hashlist -type hashItem struct { - Uid string `json:"uid"` - Hash string `json:"hash"` - Active bool -} - func InitHashList() (*apicontractsv2resources.HashList, error) { - var hashList apicontractsv2resources.HashList - rorclient := clients.RorConfig.GetRorClient() - apiHashList, err := rorclient.Resources().GetHashList(authservice.CreateRorOwnerref()) - if err != nil { - fmt.Println("Error getting hashlist from api", err) - return nil, err - } - for _, item := range apiHashList.Items { - hashList.Items = append(hashList.Items, apicontractsv2resources.HashItem{ - Uid: item.Uid, - Hash: item.Hash, - }) - } - return &hashList, nil - -} -func InitHashListv2() (*apicontractsv2resources.HashList, error) { rorclient := clients.RorConfig.GetRorClient() hashList, err := rorclient.ResourceV2().GetOwnHashes() if err != nil { @@ -49,63 +18,3 @@ func InitHashListv2() (*apicontractsv2resources.HashList, error) { return hashList, nil } - -func (hl hashList) getInactiveUid() []string { - var ret []string - if len(hl.Items) == 0 { - return ret - } - for i := range hl.Items { - if !hl.Items[i].Active { - ret = append(ret, hl.Items[i].Uid) - } - } - return ret -} - -func (hl *hashList) markActive(uid string) { - item, i := hl.getHashByUid(uid) - if item.Uid != "" { - hl.Items[i].Active = true - } - -} - -// Returns a bool value of true if the resource need to be committed -func (rc hashList) checkUpdateNeeded(uid string, hash string) bool { - hashitem, _ := rc.getHashByUid(uid) - if hashitem.Hash == hash { - rlog.Debug("No need to update, hash matched") - return false - } else { - return true - } -} -func (hl hashList) getHashByUid(uid string) (hashItem, int) { - if len(hl.Items) > 0 { - for i := range hl.Items { - if hl.Items[i].Uid == uid { - return hl.Items[i], i - } - } - } - return hashItem{}, 0 -} - -// updates hash in internal hashlist on update. The api will update its list on committing the resource to its database. -func (hl *hashList) updateHash(uid string, hash string) { - _, i := hl.getHashByUid(uid) - if i != 0 { - rlog.Debug("Update needed, hash updated", rlog.String("uid", uid)) - hl.Items[i].Hash = hash - return - } - rlog.Debug("Uid not found in hashList, adding hash", rlog.String("uid", uid)) - - newItem := hashItem{ - Uid: uid, - Hash: hash, - Active: true, - } - hl.Items = append(hl.Items, newItem) -} diff --git a/v2/internal/services/resourceupdatev2/hashlist_test.go b/v2/internal/services/resourceupdatev2/hashlist_test.go deleted file mode 100644 index 61f5a1e..0000000 --- a/v2/internal/services/resourceupdatev2/hashlist_test.go +++ /dev/null @@ -1,427 +0,0 @@ -package resourceupdatev2 - -import ( - "reflect" - "testing" - - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" - "github.com/stretchr/testify/assert" -) - -func Test_hashList_markActive(t *testing.T) { - - type fields struct { - Items []hashItem - } - type args struct { - uid string - } - - testfields := fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "1234", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - Hash: "12345", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac130022", - Hash: "12346", - Active: false, - }, - }, - } - testfieldsupdated := fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "1234", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - Hash: "12345", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac130022", - Hash: "12346", - Active: false, - }, - }, - } - - tests := []struct { - name string - fields fields - args args - want fields - }{ - { - name: "Test markActive valid input", - fields: testfields, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - }, - want: testfieldsupdated, - }, { - name: "Test markActive on unknown uid", - fields: testfields, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - }, - want: testfields, - }, { - name: "Test markActive on empty hashlist", - fields: fields{ - Items: []hashItem{}, - }, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - }, - want: fields{ - Items: []hashItem{}, - }, - }, { - name: "Test markActive on empty uid", - fields: testfields, - args: args{ - uid: "", - }, - want: testfields, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - hl := &hashList{ - Items: tt.fields.Items, - } - hl.markActive(tt.args.uid) - assert.Equal(t, hl.Items, tt.want.Items) - }) - } -} - -func Test_hashList_getHashByUid(t *testing.T) { - type fields struct { - Items []hashItem - } - type args struct { - uid string - } - tests := []struct { - name string - fields fields - args args - want hashItem - want1 int - }{ - { - name: "Test getHashByUid, empty hashlist", - fields: fields{ - Items: []hashItem{}, - }, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - }, - want: hashItem{}, - want1: 0, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - hl := hashList{ - Items: tt.fields.Items, - } - got, got1 := hl.getHashByUid(tt.args.uid) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("hashList.getHashByUid() got = %v, want %v", got, tt.want) - } - if got1 != tt.want1 { - t.Errorf("hashList.getHashByUid() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} - -func Test_hashList_getInactiveUid(t *testing.T) { - type fields struct { - Items []hashItem - } - tests := []struct { - name string - fields fields - want []string - }{ - { - name: "getInactive", - fields: fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "dabfadfd", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - Hash: "dabfadf3", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - Hash: "dab6fadf", - Active: true, - }, - }, - }, - want: []string{ - "3c99c410-3cdd-11ee-be56-0242ac120002", - "3c99c410-3cdd-11ee-be56-0242ac120012", - }, - }, { - name: "getInactive - empty result", - fields: fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "dabfadfd", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - Hash: "dabfadf3", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - Hash: "dab6fadf", - Active: true, - }, - }, - }, - want: []string{}, - }, { - name: "getInactive - empty list", - fields: fields{ - Items: []hashItem{}, - }, - want: []string{}, - }, - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - hl := hashList{ - Items: tt.fields.Items, - } - if !cmp.Equal(hl.getInactiveUid(), tt.want, cmpopts.EquateEmpty()) { - t.Errorf("%s failed", tt.name) - } - }) - } -} - -func Test_hashList_checkUpdateNeeded(t *testing.T) { - type fields struct { - Items []hashItem - } - type args struct { - uid string - hash string - } - tests := []struct { - name string - fields fields - args args - want bool - }{ - { - name: "checkUpdateNeeded - no need to update", - fields: fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "dabfadfd", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - Hash: "dabfadf3", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - Hash: "dab6fadf", - Active: true, - }, - }, - }, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - hash: "dab6fadf", - }, - want: false, - }, { - name: "checkUpdateNeeded - need to update", - fields: fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "dabfadfd", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - Hash: "dabfadf3", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - Hash: "dab6fadf", - Active: true, - }, - }, - }, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - hash: "dab6faff", - }, - want: true, - }, { - name: "checkUpdateNeeded - new uid", - fields: fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "dabfadfd", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120012", - Hash: "dabfadf3", - Active: true, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac12022", - Hash: "dab6fadf", - Active: true, - }, - }, - }, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac12032", - hash: "dab6faff", - }, - want: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - rc := hashList{ - Items: tt.fields.Items, - } - if got := rc.checkUpdateNeeded(tt.args.uid, tt.args.hash); got != tt.want { - t.Errorf("hashList.checkUpdateNeeded() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_hashList_updateHash(t *testing.T) { - - type fields struct { - Items []hashItem - } - type args struct { - uid string - hash string - } - - testfields := fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "1234", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - Hash: "12345", - Active: false, - }, - }, - } - testfieldsupdated := fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "1234", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - Hash: "123467ff", - Active: false, - }, - }, - } - testfieldsupdated2 := fields{ - Items: []hashItem{ - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120002", - Hash: "1234", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - Hash: "123467ff", - Active: false, - }, - { - Uid: "3c99c410-3cdd-11ee-be56-0242ac120032", - Hash: "123467", - Active: true, - }, - }, - } - - tests := []struct { - name string - fields fields - args args - want fields - }{ - { - name: "Test updateHash valid input", - fields: testfields, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120022", - hash: "123467ff", - }, - want: testfieldsupdated, - }, { - name: "Test updateHash add new hash", - fields: testfieldsupdated, - args: args{ - uid: "3c99c410-3cdd-11ee-be56-0242ac120032", - hash: "123467", - }, - want: testfieldsupdated2, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - hl := &hashList{ - Items: tt.fields.Items, - } - hl.updateHash(tt.args.uid, tt.args.hash) - assert.Equal(t, hl.Items, tt.want.Items) - }) - } -} diff --git a/v2/internal/services/resourceupdatev2/resourcecache.go b/v2/internal/services/resourceupdatev2/resourcecache.go index d40754c..664f047 100644 --- a/v2/internal/services/resourceupdatev2/resourcecache.go +++ b/v2/internal/services/resourceupdatev2/resourcecache.go @@ -6,9 +6,7 @@ import ( "time" "github.com/NorskHelsenett/ror-agent/v2/internal/clients" - "github.com/NorskHelsenett/ror-agent/v2/internal/services/authservice" - "github.com/NorskHelsenett/ror/pkg/apicontracts/apiresourcecontracts" "github.com/NorskHelsenett/ror/pkg/apicontracts/v2/apicontractsv2resources" "github.com/NorskHelsenett/ror/pkg/rlog" @@ -27,6 +25,7 @@ type resourcecache struct { func (rc *resourcecache) Init() error { var err error + rc.HashList, err = InitHashList() if err != nil { return err @@ -78,22 +77,13 @@ func (rc *resourcecache) finnishCleanup() { if len(inactive) == 0 { return } + rorclient := clients.RorConfig.GetRorClient() for _, uid := range inactive { rlog.Info(fmt.Sprintf("Removing resource %s", uid)) - - // rorres := rorkubernetes.NewResourceFromDynamicClient(input) - // err := rorres.SetRorMeta(rortypes.ResourceRorMeta{ - // Version: "v2", - // Ownerref: clients.RorConfig.CreateOwnerref(), - // Action: action, - // }) - - resource := apiresourcecontracts.ResourceUpdateModel{ - Owner: authservice.CreateOwnerref(), - Uid: uid, - Action: apiresourcecontracts.K8sActionDelete, + _, err := rorclient.ResourceV2().Delete(context.Background(), uid) + if err != nil { + rlog.Error(fmt.Sprintf("Error removing resource %s", uid), err) } - _ = sendResourceUpdateToRor(&resource) } rlog.Info(fmt.Sprintf("resource cleanup done, %d resources removed", len(inactive))) }