From 9e7725f451634d07d636a561348f543a2e42152d Mon Sep 17 00:00:00 2001 From: linyguo Date: Wed, 27 Nov 2024 16:15:19 +0800 Subject: [PATCH] update etag --- .../managers/activations/activations-manager.go | 11 +++++------ .../campaign-container-manager.go | 13 ++++++------- .../managers/campaigns/campaigns-manager.go | 12 ++++++------ .../catalogcontainers/catalog-container-manager.go | 13 ++++++------- .../v1alpha1/managers/catalogs/catalogs-manager.go | 11 +++++------ .../v1alpha1/managers/devices/devices-manager.go | 12 ++++++------ .../managers/devices/devices-manager_test.go | 1 - .../managers/instances/instances-manager.go | 11 +++++------ .../apis/v1alpha1/managers/models/models-manager.go | 12 ++++++------ .../apis/v1alpha1/managers/skills/skills-manager.go | 12 ++++++------ .../solution-container-manager.go | 13 ++++++------- .../managers/solutions/solutions-manager.go | 12 ++++++------ .../v1alpha1/managers/targets/targets-manager.go | 11 +++++------ api/pkg/apis/v1alpha1/model/objectmeta.go | 6 ++++-- api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go | 2 ++ 15 files changed, 74 insertions(+), 78 deletions(-) diff --git a/api/pkg/apis/v1alpha1/managers/activations/activations-manager.go b/api/pkg/apis/v1alpha1/managers/activations/activations-manager.go index 6c5ab5baf..07665453f 100644 --- a/api/pkg/apis/v1alpha1/managers/activations/activations-manager.go +++ b/api/pkg/apis/v1alpha1/managers/activations/activations-manager.go @@ -93,7 +93,7 @@ func (m *ActivationsManager) GetState(ctx context.Context, name string, namespac return model.ActivationState{}, err } var ret model.ActivationState - ret, err = getActivationState(entry.Body, entry.ETag) + ret, err = getActivationState(entry.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to activation state for %s in namespace %s: %v", name, namespace, err) return model.ActivationState{}, err @@ -101,7 +101,7 @@ func (m *ActivationsManager) GetState(ctx context.Context, name string, namespac return ret, nil } -func getActivationState(body interface{}, etag string) (model.ActivationState, error) { +func getActivationState(body interface{}) (model.ActivationState, error) { var activationState model.ActivationState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &activationState) @@ -111,7 +111,6 @@ func getActivationState(body interface{}, etag string) (model.ActivationState, e if activationState.Spec == nil { activationState.Spec = &model.ActivationSpec{} } - activationState.ObjectMeta.ETag = etag if activationState.Status == nil { activationState.Status = &model.ActivationStatus{} } @@ -148,12 +147,12 @@ func (m *ActivationsManager) UpsertState(ctx context.Context, name string, state entry, err = m.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getActivationState(entry.Body, entry.ETag) + itemState, err := getActivationState(entry.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to activation state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if m.needValidate { @@ -243,7 +242,7 @@ func (t *ActivationsManager) ListState(ctx context.Context, namespace string) ([ ret := make([]model.ActivationState, 0) for _, t := range activations { var rt model.ActivationState - rt, err = getActivationState(t.Body, t.ETag) + rt, err = getActivationState(t.Body) if err != nil { return nil, err } diff --git a/api/pkg/apis/v1alpha1/managers/campaigncontainers/campaign-container-manager.go b/api/pkg/apis/v1alpha1/managers/campaigncontainers/campaign-container-manager.go index 3836ce3aa..e95525e2a 100644 --- a/api/pkg/apis/v1alpha1/managers/campaigncontainers/campaign-container-manager.go +++ b/api/pkg/apis/v1alpha1/managers/campaigncontainers/campaign-container-manager.go @@ -91,11 +91,11 @@ func (t *CampaignContainersManager) UpsertState(ctx context.Context, name string Campaign, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getCampaignContainerState(Campaign.Body, Campaign.ETag) + itemState, err := getCampaignContainerState(Campaign.Body) if err != nil { return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } body := map[string]interface{}{ @@ -109,7 +109,7 @@ func (t *CampaignContainersManager) UpsertState(ctx context.Context, name string Value: states.StateEntry{ ID: name, Body: body, - ETag: "", + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -151,7 +151,7 @@ func (t *CampaignContainersManager) ListState(ctx context.Context, namespace str ret := make([]model.CampaignContainerState, 0) for _, t := range campaigncontainers { var rt model.CampaignContainerState - rt, err = getCampaignContainerState(t.Body, t.ETag) + rt, err = getCampaignContainerState(t.Body) if err != nil { return nil, err } @@ -160,7 +160,7 @@ func (t *CampaignContainersManager) ListState(ctx context.Context, namespace str return ret, nil } -func getCampaignContainerState(body interface{}, etag string) (model.CampaignContainerState, error) { +func getCampaignContainerState(body interface{}) (model.CampaignContainerState, error) { var CampaignContainerState model.CampaignContainerState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &CampaignContainerState) @@ -170,7 +170,6 @@ func getCampaignContainerState(body interface{}, etag string) (model.CampaignCon if CampaignContainerState.Spec == nil { CampaignContainerState.Spec = &model.CampaignContainerSpec{} } - CampaignContainerState.ObjectMeta.ETag = etag return CampaignContainerState, nil } @@ -198,7 +197,7 @@ func (t *CampaignContainersManager) GetState(ctx context.Context, id string, nam return model.CampaignContainerState{}, err } var ret model.CampaignContainerState - ret, err = getCampaignContainerState(Campaign.Body, Campaign.ETag) + ret, err = getCampaignContainerState(Campaign.Body) if err != nil { return model.CampaignContainerState{}, err } diff --git a/api/pkg/apis/v1alpha1/managers/campaigns/campaigns-manager.go b/api/pkg/apis/v1alpha1/managers/campaigns/campaigns-manager.go index a0c05cdae..c97a54bb6 100644 --- a/api/pkg/apis/v1alpha1/managers/campaigns/campaigns-manager.go +++ b/api/pkg/apis/v1alpha1/managers/campaigns/campaigns-manager.go @@ -80,7 +80,7 @@ func (m *CampaignsManager) GetState(ctx context.Context, name string, namespace return model.CampaignState{}, err } var ret model.CampaignState - ret, err = getCampaignState(entry.Body, entry.ETag) + ret, err = getCampaignState(entry.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to campaign state for %s in namespace %s: %v", name, namespace, err) return model.CampaignState{}, err @@ -88,7 +88,7 @@ func (m *CampaignsManager) GetState(ctx context.Context, name string, namespace return ret, nil } -func getCampaignState(body interface{}, etag string) (model.CampaignState, error) { +func getCampaignState(body interface{}) (model.CampaignState, error) { var campaignState model.CampaignState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &campaignState) @@ -98,7 +98,6 @@ func getCampaignState(body interface{}, etag string) (model.CampaignState, error if campaignState.Spec == nil { campaignState.Spec = &model.CampaignSpec{} } - campaignState.ObjectMeta.ETag = etag return campaignState, nil } @@ -129,12 +128,12 @@ func (m *CampaignsManager) UpsertState(ctx context.Context, name string, state m entry, err := m.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getCampaignState(entry.Body, entry.ETag) + itemState, err := getCampaignState(entry.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to campaign state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if m.needValidate { @@ -158,6 +157,7 @@ func (m *CampaignsManager) UpsertState(ctx context.Context, name string, state m "metadata": state.ObjectMeta, "spec": state.Spec, }, + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -225,7 +225,7 @@ func (t *CampaignsManager) ListState(ctx context.Context, namespace string) ([]m ret := make([]model.CampaignState, 0) for _, t := range campaigns { var rt model.CampaignState - rt, err = getCampaignState(t.Body, t.ETag) + rt, err = getCampaignState(t.Body) if err != nil { return nil, err } diff --git a/api/pkg/apis/v1alpha1/managers/catalogcontainers/catalog-container-manager.go b/api/pkg/apis/v1alpha1/managers/catalogcontainers/catalog-container-manager.go index a67ec4121..d3e277749 100644 --- a/api/pkg/apis/v1alpha1/managers/catalogcontainers/catalog-container-manager.go +++ b/api/pkg/apis/v1alpha1/managers/catalogcontainers/catalog-container-manager.go @@ -91,11 +91,11 @@ func (t *CatalogContainersManager) UpsertState(ctx context.Context, name string, item, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getCatalogContainerState(item.Body, item.ETag) + itemState, err := getCatalogContainerState(item.Body) if err != nil { return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } body := map[string]interface{}{ @@ -109,7 +109,7 @@ func (t *CatalogContainersManager) UpsertState(ctx context.Context, name string, Value: states.StateEntry{ ID: name, Body: body, - ETag: "", + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -151,7 +151,7 @@ func (t *CatalogContainersManager) ListState(ctx context.Context, namespace stri ret := make([]model.CatalogContainerState, 0) for _, t := range catalogcontainers { var rt model.CatalogContainerState - rt, err = getCatalogContainerState(t.Body, t.ETag) + rt, err = getCatalogContainerState(t.Body) if err != nil { return nil, err } @@ -160,7 +160,7 @@ func (t *CatalogContainersManager) ListState(ctx context.Context, namespace stri return ret, nil } -func getCatalogContainerState(body interface{}, etag string) (model.CatalogContainerState, error) { +func getCatalogContainerState(body interface{}) (model.CatalogContainerState, error) { var CatalogContainerState model.CatalogContainerState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &CatalogContainerState) @@ -170,7 +170,6 @@ func getCatalogContainerState(body interface{}, etag string) (model.CatalogConta if CatalogContainerState.Spec == nil { CatalogContainerState.Spec = &model.CatalogContainerSpec{} } - CatalogContainerState.ObjectMeta.ETag = etag return CatalogContainerState, nil } @@ -198,7 +197,7 @@ func (t *CatalogContainersManager) GetState(ctx context.Context, id string, name return model.CatalogContainerState{}, err } var ret model.CatalogContainerState - ret, err = getCatalogContainerState(Campaign.Body, Campaign.ETag) + ret, err = getCatalogContainerState(Campaign.Body) if err != nil { return model.CatalogContainerState{}, err } diff --git a/api/pkg/apis/v1alpha1/managers/catalogs/catalogs-manager.go b/api/pkg/apis/v1alpha1/managers/catalogs/catalogs-manager.go index 53a9e17c0..3aab514d7 100644 --- a/api/pkg/apis/v1alpha1/managers/catalogs/catalogs-manager.go +++ b/api/pkg/apis/v1alpha1/managers/catalogs/catalogs-manager.go @@ -85,14 +85,14 @@ func (s *CatalogsManager) GetState(ctx context.Context, name string, namespace s return model.CatalogState{}, err } var ret model.CatalogState - ret, err = getCatalogState(entry.Body, entry.ETag) + ret, err = getCatalogState(entry.Body) if err != nil { return model.CatalogState{}, err } return ret, nil } -func getCatalogState(body interface{}, etag string) (model.CatalogState, error) { +func getCatalogState(body interface{}) (model.CatalogState, error) { var catalogState model.CatalogState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &catalogState) @@ -102,7 +102,6 @@ func getCatalogState(body interface{}, etag string) (model.CatalogState, error) if catalogState.Spec == nil { catalogState.Spec = &model.CatalogSpec{} } - catalogState.ObjectMeta.ETag = etag if catalogState.Status == nil { catalogState.Status = &model.CatalogStatus{} } @@ -135,11 +134,11 @@ func (m *CatalogsManager) UpsertState(ctx context.Context, name string, state mo entry, err := m.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getCatalogState(entry.Body, entry.ETag) + itemState, err := getCatalogState(entry.Body) if err != nil { return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if m.needValidate { @@ -249,7 +248,7 @@ func (t *CatalogsManager) ListState(ctx context.Context, namespace string, filte ret := make([]model.CatalogState, 0) for _, t := range catalogs { var rt model.CatalogState - rt, err = getCatalogState(t.Body, t.ETag) + rt, err = getCatalogState(t.Body) if err != nil { return nil, err } diff --git a/api/pkg/apis/v1alpha1/managers/devices/devices-manager.go b/api/pkg/apis/v1alpha1/managers/devices/devices-manager.go index 63ca10d94..b0037ccbd 100644 --- a/api/pkg/apis/v1alpha1/managers/devices/devices-manager.go +++ b/api/pkg/apis/v1alpha1/managers/devices/devices-manager.go @@ -98,12 +98,12 @@ func (t *DevicesManager) UpsertState(ctx context.Context, name string, state mod entry, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - ret, err := getDeviceState(entry.Body, entry.ETag) + ret, err := getDeviceState(entry.Body) if err != nil { log.ErrorfCtx(ctx, " M (Devices): GetSpec failed to get device state, error: %v", err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(ret.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(ret.ObjectMeta) } upsertRequest := states.UpsertRequest{ @@ -115,6 +115,7 @@ func (t *DevicesManager) UpsertState(ctx context.Context, name string, state mod "metadata": state.ObjectMeta, "spec": state.Spec, }, + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -159,7 +160,7 @@ func (t *DevicesManager) ListState(ctx context.Context, namespace string) ([]mod ret := make([]model.DeviceState, 0) for _, t := range devices { var rt model.DeviceState - rt, err = getDeviceState(t.Body, t.ETag) + rt, err = getDeviceState(t.Body) if err != nil { log.ErrorfCtx(ctx, " M (Devices): ListState failed to get device state %s, error: %v", t.ID, err) return nil, err @@ -169,7 +170,7 @@ func (t *DevicesManager) ListState(ctx context.Context, namespace string) ([]mod return ret, nil } -func getDeviceState(body interface{}, etag string) (model.DeviceState, error) { +func getDeviceState(body interface{}) (model.DeviceState, error) { var deviceState model.DeviceState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &deviceState) @@ -179,7 +180,6 @@ func getDeviceState(body interface{}, etag string) (model.DeviceState, error) { if deviceState.Spec == nil { deviceState.Spec = &model.DeviceSpec{} } - deviceState.ObjectMeta.ETag = etag return deviceState, nil } @@ -209,7 +209,7 @@ func (t *DevicesManager) GetState(ctx context.Context, name string, namespace st return model.DeviceState{}, err } var ret model.DeviceState - ret, err = getDeviceState(entry.Body, entry.ETag) + ret, err = getDeviceState(entry.Body) if err != nil { log.ErrorfCtx(ctx, " M (Devices): GetSpec failed to get device state, error: %v", err) return model.DeviceState{}, err diff --git a/api/pkg/apis/v1alpha1/managers/devices/devices-manager_test.go b/api/pkg/apis/v1alpha1/managers/devices/devices-manager_test.go index d283ddd91..faf040867 100644 --- a/api/pkg/apis/v1alpha1/managers/devices/devices-manager_test.go +++ b/api/pkg/apis/v1alpha1/managers/devices/devices-manager_test.go @@ -85,7 +85,6 @@ func TestUpsertAndGet(t *testing.T) { ObjectMeta: model.ObjectMeta{ Name: "test", Namespace: "default", - ETag: "1", }, Spec: &model.DeviceSpec{ DisplayName: "device", diff --git a/api/pkg/apis/v1alpha1/managers/instances/instances-manager.go b/api/pkg/apis/v1alpha1/managers/instances/instances-manager.go index 8dbe32b0a..0ca2eb34f 100644 --- a/api/pkg/apis/v1alpha1/managers/instances/instances-manager.go +++ b/api/pkg/apis/v1alpha1/managers/instances/instances-manager.go @@ -102,12 +102,12 @@ func (t *InstancesManager) UpsertState(ctx context.Context, name string, state m item, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getInstanceState(item.Body, item.ETag) + itemState, err := getInstanceState(item.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to instance state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if t.needValidate { @@ -177,7 +177,7 @@ func (t *InstancesManager) ListState(ctx context.Context, namespace string) ([]m ret := make([]model.InstanceState, 0) for _, t := range instances { var rt model.InstanceState - rt, err = getInstanceState(t.Body, t.ETag) + rt, err = getInstanceState(t.Body) if err != nil { return nil, err } @@ -186,7 +186,7 @@ func (t *InstancesManager) ListState(ctx context.Context, namespace string) ([]m return ret, nil } -func getInstanceState(body interface{}, etag string) (model.InstanceState, error) { +func getInstanceState(body interface{}) (model.InstanceState, error) { var instanceState model.InstanceState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &instanceState) @@ -196,7 +196,6 @@ func getInstanceState(body interface{}, etag string) (model.InstanceState, error if instanceState.Spec == nil { instanceState.Spec = &model.InstanceSpec{} } - instanceState.ObjectMeta.ETag = etag return instanceState, nil } @@ -224,7 +223,7 @@ func (t *InstancesManager) GetState(ctx context.Context, id string, namespace st return model.InstanceState{}, err } var ret model.InstanceState - ret, err = getInstanceState(instance.Body, instance.ETag) + ret, err = getInstanceState(instance.Body) if err != nil { return model.InstanceState{}, err } diff --git a/api/pkg/apis/v1alpha1/managers/models/models-manager.go b/api/pkg/apis/v1alpha1/managers/models/models-manager.go index 8348dc6f6..dce7d7a91 100644 --- a/api/pkg/apis/v1alpha1/managers/models/models-manager.go +++ b/api/pkg/apis/v1alpha1/managers/models/models-manager.go @@ -94,12 +94,12 @@ func (t *ModelsManager) UpsertState(ctx context.Context, name string, state mode m, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - ret, err := getModelState(m.Body, m.ETag) + ret, err := getModelState(m.Body) if err != nil { log.ErrorfCtx(ctx, " M (Models): failed to convert to model state, name: %s, err: %v", name, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(ret.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(ret.ObjectMeta) } upsertRequest := states.UpsertRequest{ @@ -111,6 +111,7 @@ func (t *ModelsManager) UpsertState(ctx context.Context, name string, state mode "metadata": state.ObjectMeta, "spec": state.Spec, }, + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -155,7 +156,7 @@ func (t *ModelsManager) ListState(ctx context.Context, namespace string) ([]mode ret := make([]model.ModelState, 0) for _, t := range models { var rt model.ModelState - rt, err = getModelState(t.Body, t.ETag) + rt, err = getModelState(t.Body) if err != nil { log.ErrorfCtx(ctx, " M (Models): failed to getModelState, err: %v", err) return nil, err @@ -165,7 +166,7 @@ func (t *ModelsManager) ListState(ctx context.Context, namespace string) ([]mode return ret, nil } -func getModelState(body interface{}, etag string) (model.ModelState, error) { +func getModelState(body interface{}) (model.ModelState, error) { var modelState model.ModelState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &modelState) @@ -175,7 +176,6 @@ func getModelState(body interface{}, etag string) (model.ModelState, error) { if modelState.Spec == nil { modelState.Spec = &model.ModelSpec{} } - modelState.ObjectMeta.ETag = etag return modelState, nil } @@ -206,7 +206,7 @@ func (t *ModelsManager) GetState(ctx context.Context, name string, namespace str } var ret model.ModelState - ret, err = getModelState(m.Body, m.ETag) + ret, err = getModelState(m.Body) if err != nil { log.ErrorfCtx(ctx, " M (Models): failed to getModelState, name: %s, err: %v", name, err) return model.ModelState{}, err diff --git a/api/pkg/apis/v1alpha1/managers/skills/skills-manager.go b/api/pkg/apis/v1alpha1/managers/skills/skills-manager.go index b4473c922..0833b0712 100644 --- a/api/pkg/apis/v1alpha1/managers/skills/skills-manager.go +++ b/api/pkg/apis/v1alpha1/managers/skills/skills-manager.go @@ -92,12 +92,12 @@ func (t *SkillsManager) UpsertState(ctx context.Context, name string, state mode m, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - ret, err := getSkillState(m.Body, m.ETag) + ret, err := getSkillState(m.Body) if err != nil { log.ErrorfCtx(ctx, " M (Skills): failed to get skill state, name: %s, err: %v", name, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(ret.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(ret.ObjectMeta) } upsertRequest := states.UpsertRequest{ @@ -109,6 +109,7 @@ func (t *SkillsManager) UpsertState(ctx context.Context, name string, state mode "metadata": state.ObjectMeta, "spec": state.Spec, }, + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -153,7 +154,7 @@ func (t *SkillsManager) ListState(ctx context.Context, namespace string) ([]mode ret := make([]model.SkillState, 0) for _, t := range models { var rt model.SkillState - rt, err = getSkillState(t.Body, t.ETag) + rt, err = getSkillState(t.Body) if err != nil { log.ErrorfCtx(ctx, " M (Models): failed to get skill state, err: %v", err) return nil, err @@ -163,7 +164,7 @@ func (t *SkillsManager) ListState(ctx context.Context, namespace string) ([]mode return ret, nil } -func getSkillState(body interface{}, etag string) (model.SkillState, error) { +func getSkillState(body interface{}) (model.SkillState, error) { var skillState model.SkillState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &skillState) @@ -173,7 +174,6 @@ func getSkillState(body interface{}, etag string) (model.SkillState, error) { if skillState.Spec == nil { skillState.Spec = &model.SkillSpec{} } - skillState.ObjectMeta.ETag = etag return skillState, nil } @@ -203,7 +203,7 @@ func (t *SkillsManager) GetState(ctx context.Context, name string, namespace str return model.SkillState{}, err } var ret model.SkillState - ret, err = getSkillState(m.Body, m.ETag) + ret, err = getSkillState(m.Body) if err != nil { log.ErrorfCtx(ctx, " M (Skills): failed to get skill state, name: %s, err: %v", name, err) return model.SkillState{}, err diff --git a/api/pkg/apis/v1alpha1/managers/solutioncontainers/solution-container-manager.go b/api/pkg/apis/v1alpha1/managers/solutioncontainers/solution-container-manager.go index 5d5f4dada..baf30b32c 100644 --- a/api/pkg/apis/v1alpha1/managers/solutioncontainers/solution-container-manager.go +++ b/api/pkg/apis/v1alpha1/managers/solutioncontainers/solution-container-manager.go @@ -91,11 +91,11 @@ func (t *SolutionContainersManager) UpsertState(ctx context.Context, name string Solution, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve existing object annotations - ret, err := getSolutionContainerState(Solution.Body, Solution.ETag) + ret, err := getSolutionContainerState(Solution.Body) if err != nil { return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(ret.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(ret.ObjectMeta) } body := map[string]interface{}{ @@ -109,7 +109,7 @@ func (t *SolutionContainersManager) UpsertState(ctx context.Context, name string Value: states.StateEntry{ ID: name, Body: body, - ETag: "", + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -151,7 +151,7 @@ func (t *SolutionContainersManager) ListState(ctx context.Context, namespace str ret := make([]model.SolutionContainerState, 0) for _, t := range solutioncontainers { var rt model.SolutionContainerState - rt, err = getSolutionContainerState(t.Body, t.ETag) + rt, err = getSolutionContainerState(t.Body) if err != nil { return nil, err } @@ -160,7 +160,7 @@ func (t *SolutionContainersManager) ListState(ctx context.Context, namespace str return ret, nil } -func getSolutionContainerState(body interface{}, etag string) (model.SolutionContainerState, error) { +func getSolutionContainerState(body interface{}) (model.SolutionContainerState, error) { var SolutionContainerState model.SolutionContainerState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &SolutionContainerState) @@ -170,7 +170,6 @@ func getSolutionContainerState(body interface{}, etag string) (model.SolutionCon if SolutionContainerState.Spec == nil { SolutionContainerState.Spec = &model.SolutionContainerSpec{} } - SolutionContainerState.ObjectMeta.ETag = etag return SolutionContainerState, nil } @@ -198,7 +197,7 @@ func (t *SolutionContainersManager) GetState(ctx context.Context, id string, nam return model.SolutionContainerState{}, err } var ret model.SolutionContainerState - ret, err = getSolutionContainerState(Solution.Body, Solution.ETag) + ret, err = getSolutionContainerState(Solution.Body) if err != nil { return model.SolutionContainerState{}, err } diff --git a/api/pkg/apis/v1alpha1/managers/solutions/solutions-manager.go b/api/pkg/apis/v1alpha1/managers/solutions/solutions-manager.go index 659f3a4e8..99065953e 100644 --- a/api/pkg/apis/v1alpha1/managers/solutions/solutions-manager.go +++ b/api/pkg/apis/v1alpha1/managers/solutions/solutions-manager.go @@ -107,12 +107,12 @@ func (t *SolutionsManager) UpsertState(ctx context.Context, name string, state m } item, err := t.StateProvider.Get(ctx, getRequest) if err == nil { - itemState, err := getSolutionState(item.Body, item.ETag) + itemState, err := getSolutionState(item.Body) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to solution state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if t.needValidate { @@ -138,6 +138,7 @@ func (t *SolutionsManager) UpsertState(ctx context.Context, name string, state m Value: states.StateEntry{ ID: name, Body: body, + ETag: state.ObjectMeta.ETag, }, Metadata: map[string]interface{}{ "namespace": state.ObjectMeta.Namespace, @@ -177,7 +178,7 @@ func (t *SolutionsManager) ListState(ctx context.Context, namespace string) ([]m ret := make([]model.SolutionState, 0) for _, t := range solutions { var rt model.SolutionState - rt, err = getSolutionState(t.Body, t.ETag) + rt, err = getSolutionState(t.Body) if err != nil { return nil, err } @@ -186,7 +187,7 @@ func (t *SolutionsManager) ListState(ctx context.Context, namespace string) ([]m return ret, nil } -func getSolutionState(body interface{}, etag string) (model.SolutionState, error) { +func getSolutionState(body interface{}) (model.SolutionState, error) { var solutionState model.SolutionState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &solutionState) @@ -196,7 +197,6 @@ func getSolutionState(body interface{}, etag string) (model.SolutionState, error if solutionState.Spec == nil { solutionState.Spec = &model.SolutionSpec{} } - solutionState.ObjectMeta.ETag = etag return solutionState, nil } @@ -224,7 +224,7 @@ func (t *SolutionsManager) GetState(ctx context.Context, id string, namespace st return model.SolutionState{}, err } var ret model.SolutionState - ret, err = getSolutionState(target.Body, target.ETag) + ret, err = getSolutionState(target.Body) if err != nil { return model.SolutionState{}, err } diff --git a/api/pkg/apis/v1alpha1/managers/targets/targets-manager.go b/api/pkg/apis/v1alpha1/managers/targets/targets-manager.go index 251951139..96c7b9c41 100644 --- a/api/pkg/apis/v1alpha1/managers/targets/targets-manager.go +++ b/api/pkg/apis/v1alpha1/managers/targets/targets-manager.go @@ -109,12 +109,12 @@ func (t *TargetsManager) UpsertState(ctx context.Context, name string, state mod item, err := t.StateProvider.Get(ctx, getRequest) if err == nil { // preserve system annotations for existing object - itemState, err := getTargetState(item, item.ETag) + itemState, err := getTargetState(item) if err != nil { log.ErrorfCtx(ctx, "Failed to convert to target state for %s in namespace %s: %v", name, state.ObjectMeta.Namespace, err) return err } - state.ObjectMeta.PreserveSystemMetadataAnnotations(itemState.ObjectMeta.Annotations) + state.ObjectMeta.PreserveSystemMetadata(itemState.ObjectMeta) } if t.needValidate { @@ -242,7 +242,7 @@ func (t *TargetsManager) ListState(ctx context.Context, namespace string) ([]mod ret := make([]model.TargetState, 0) for _, t := range targets { var rt model.TargetState - rt, err = getTargetState(t.Body, t.ETag) + rt, err = getTargetState(t.Body) if err != nil { return nil, err } @@ -251,7 +251,7 @@ func (t *TargetsManager) ListState(ctx context.Context, namespace string) ([]mod return ret, nil } -func getTargetState(body interface{}, etag string) (model.TargetState, error) { +func getTargetState(body interface{}) (model.TargetState, error) { var targetState model.TargetState bytes, _ := json.Marshal(body) err := json.Unmarshal(bytes, &targetState) @@ -261,7 +261,6 @@ func getTargetState(body interface{}, etag string) (model.TargetState, error) { if targetState.Spec == nil { targetState.Spec = &model.TargetSpec{} } - targetState.ObjectMeta.ETag = etag return targetState, nil } @@ -290,7 +289,7 @@ func (t *TargetsManager) GetState(ctx context.Context, id string, namespace stri } var ret model.TargetState - ret, err = getTargetState(target.Body, target.ETag) + ret, err = getTargetState(target.Body) if err != nil { return model.TargetState{}, err } diff --git a/api/pkg/apis/v1alpha1/model/objectmeta.go b/api/pkg/apis/v1alpha1/model/objectmeta.go index 362aabfef..c59ae4b51 100644 --- a/api/pkg/apis/v1alpha1/model/objectmeta.go +++ b/api/pkg/apis/v1alpha1/model/objectmeta.go @@ -121,15 +121,17 @@ func (c *ObjectMeta) UpdateAnnotation(key string, value string) { c.Annotations[key] = value } -func (c *ObjectMeta) PreserveSystemMetadataAnnotations(annotations map[string]string) { +func (c *ObjectMeta) PreserveSystemMetadata(metadata ObjectMeta) { if c.Annotations == nil { c.Annotations = make(map[string]string) } // system annotations should only be updated ineternally for _, key := range constants.SystemReservedAnnotations() { - if value, ok := annotations[key]; ok { + if value, ok := metadata.Annotations[key]; ok { c.Annotations[key] = value } } + // set the resoure version + c.ETag = metadata.ETag } diff --git a/api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go b/api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go index bb6de4623..9c030e3e8 100644 --- a/api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go +++ b/api/pkg/apis/v1alpha1/providers/states/k8s/k8s.go @@ -419,6 +419,7 @@ func (s *K8sStateProvider) List(ctx context.Context, request states.ListRequest) Name: v.GetName(), Namespace: v.GetNamespace(), Labels: v.GetLabels(), + ETag: v.GetResourceVersion(), Annotations: v.GetAnnotations(), ObjGeneration: v.GetGeneration(), } @@ -520,6 +521,7 @@ func (s *K8sStateProvider) Get(ctx context.Context, request states.GetRequest) ( Name: item.GetName(), Namespace: item.GetNamespace(), Labels: item.GetLabels(), + ETag: item.GetResourceVersion(), Annotations: item.GetAnnotations(), ObjGeneration: item.GetGeneration(), }