From 614efbb55f6c7b18788cc6773a953fe3a918701f Mon Sep 17 00:00:00 2001 From: Srinivas Atmakuri Date: Thu, 2 Mar 2023 02:27:18 +0530 Subject: [PATCH] API OperatorFlagsMergeStrategy JIRA: https://issues.redhat.com/browse/ARO-1885 --- pkg/api/admin/operatorflags.go | 63 +++++++++++++++++++++ pkg/frontend/openshiftcluster_putorpatch.go | 4 ++ 2 files changed, 67 insertions(+) create mode 100644 pkg/api/admin/operatorflags.go diff --git a/pkg/api/admin/operatorflags.go b/pkg/api/admin/operatorflags.go new file mode 100644 index 00000000000..7f4163fcbec --- /dev/null +++ b/pkg/api/admin/operatorflags.go @@ -0,0 +1,63 @@ +package admin + +import ( + "encoding/json" + "net/http" + + "github.com/Azure/ARO-RP/pkg/api" +) + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +type operatorFlagsMergeStrategyStruct struct { + OperatorFlagsMergeStrategy string + Cluster *api.OpenShiftCluster +} + +const ( + operatorFlagsMergeStrategyDefault string = "merge" + operatorFlagsMergeStrategyMerge string = "merge" + operatorFlagsMergeStrategyReset string = "reset" +) + +// When a cluster is edited via the PATCH Cluster Geneva Action (aka an Admin Update) +// the flags given are treated according to the provided Update Strategy, +// provided in operatorFlagsMergeStrategy + +// merge (default): The provided cluster flags are laid on top of the cluster’s existing flags. +// reset: The provided cluster flags are laid on top of the default cluster flags, +// essentially ‘resetting’ the flags if no new flags are provided. +func OperatorFlagsMergeStrategy(oc *api.OpenShiftCluster, body []byte) error { + payload := operatorFlagsMergeStrategyStruct{ + OperatorFlagsMergeStrategy: operatorFlagsMergeStrategyDefault, + Cluster: &api.OpenShiftCluster{}, + } + + err := json.Unmarshal(body, &payload) + if err != nil { + return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidRequestContent, "", "The request content was invalid and could not be deserialized: %q.", err) + } + + // return if payload is empty + if properties := &payload.Cluster.Properties; payload.Cluster == nil || properties == nil || payload.Cluster.Properties.OperatorFlags == nil { + return nil + } + // return error if OperatorFlagsMergeStrategy is not merge or reset, default is merge + if payload.OperatorFlagsMergeStrategy != operatorFlagsMergeStrategyMerge && + payload.OperatorFlagsMergeStrategy != operatorFlagsMergeStrategyReset { + return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, "", "invalid operatorFlagsMergeStrategy '%s', can only be 'merge' or 'reset'", payload.OperatorFlagsMergeStrategy) + } + // return nil, if OperatorFlagsMergeStrategy is merge and payload has not operatorFlags + // return operatorFlags of payload, if OperatorFlagsMergeStrategy is merge and payload has operatorFlags + // return defaultOperatorFlags, if OperatorFlagsMergeStrategy is reset and payload has not operatorFlags + // return defaultOperatorFlags + operatorFlags of payload, if OperatorFlagsMergeStrategy is reset and payload has operatorFlags + if payload.OperatorFlagsMergeStrategy == operatorFlagsMergeStrategyReset { + oc.Properties.OperatorFlags = api.DefaultOperatorFlags() + for operatorflag, value := range payload.Cluster.Properties.OperatorFlags { + oc.Properties.OperatorFlags[operatorflag] = value + } + } + + return nil +} diff --git a/pkg/frontend/openshiftcluster_putorpatch.go b/pkg/frontend/openshiftcluster_putorpatch.go index 047118cbb69..5ada900bbef 100644 --- a/pkg/frontend/openshiftcluster_putorpatch.go +++ b/pkg/frontend/openshiftcluster_putorpatch.go @@ -148,6 +148,10 @@ func (f *frontend) _putOrPatchOpenShiftCluster(ctx context.Context, log *logrus. // provide single field json to be updated in the database. // Patch should be used for updating individual fields of the document. case http.MethodPatch: + err = admin.OperatorFlagsMergeStrategy(doc.OpenShiftCluster, body) + if err != nil { + return nil, err + } ext = converter.ToExternal(doc.OpenShiftCluster) }