Skip to content

Commit

Permalink
add config-spec-hash annotation to manifestwork
Browse files Browse the repository at this point in the history
Signed-off-by: haoqing0110 <[email protected]>
  • Loading branch information
haoqing0110 committed Mar 14, 2023
1 parent 613d134 commit cca7df9
Show file tree
Hide file tree
Showing 36 changed files with 1,441 additions and 588 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module open-cluster-management.io/addon-framework

go 1.19

replace open-cluster-management.io/api => github.com/haoqing0110/api v0.0.0-20230314030136-5f4cd436ed1a

require (
github.com/evanphx/json-patch v4.12.0+incompatible
github.com/fatih/structs v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/haoqing0110/api v0.0.0-20230314030136-5f4cd436ed1a h1:eON8smxYh6/LI6q/ljrmwAxYP37m9AMygprWXzpN4gY=
github.com/haoqing0110/api v0.0.0-20230314030136-5f4cd436ed1a/go.mod h1:TjWobG3dTZJf/Ye04358/F/381RjE/+HXVDGMnZBpjc=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -805,8 +807,6 @@ k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+O
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280/go.mod h1:+Axhij7bCpeqhklhUTe3xmOn6bWxolyZEeyaFpjGtl4=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 h1:KTgPnR10d5zhztWptI952TNtt/4u5h3IzDXkdIMuo2Y=
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
open-cluster-management.io/api v0.10.1-0.20230216050312-b716d4a5c514 h1:jtodRlbcZdvtFzzBDjwJ2XfjaLn0XMpRXBZUsTRMcnE=
open-cluster-management.io/api v0.10.1-0.20230216050312-b716d4a5c514/go.mod h1:3u4r42Zbg0utOdgarQEgAR35NAKcHgkt9xZWUTW6VAw=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
77 changes: 77 additions & 0 deletions pkg/addonmanager/controllers/agentdeploy/util_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
package agentdeploy

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
workapiv1 "open-cluster-management.io/api/work/v1"
)

func TestConfigsToAnnotations(t *testing.T) {
cases := []struct {
name string
configReference []addonapiv1alpha1.ConfigReference
expectAnnotations map[string]string
}{
{
name: "generate annotaions",
configReference: []addonapiv1alpha1.ConfigReference{
{
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
Group: "addon.open-cluster-management.io",
Resource: "addondeploymentconfigs",
},
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
ConfigReferent: addonapiv1alpha1.ConfigReferent{
Name: "test",
Namespace: "open-cluster-management",
},
SpecHash: "hash1",
},
},
{
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
Resource: "addonhubconfigs",
},
DesiredConfig: &addonapiv1alpha1.ConfigSpecHash{
ConfigReferent: addonapiv1alpha1.ConfigReferent{
Name: "test",
},
SpecHash: "hash2",
},
},
},
expectAnnotations: map[string]string{
workapiv1.ManifestConfigSpecHashAnnotationKey: `{"addondeploymentconfigs.addon.open-cluster-management.io/open-cluster-management/test":"hash1","addonhubconfigs//test":"hash2"}`},
},
{
name: "generate annotaions without configReference",
configReference: []addonapiv1alpha1.ConfigReference{},
expectAnnotations: nil,
},
{
name: "generate annotaions without DesiredConfig",
configReference: []addonapiv1alpha1.ConfigReference{
{
ConfigGroupResource: addonapiv1alpha1.ConfigGroupResource{
Group: "addon.open-cluster-management.io",
Resource: "addondeploymentconfigs",
},
},
},
expectAnnotations: map[string]string{
workapiv1.ManifestConfigSpecHashAnnotationKey: `{}`},
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
annotations, err := configsToAnnotations(c.configReference)
assert.NoError(t, err)
if !reflect.DeepEqual(annotations, c.expectAnnotations) {
t.Fatalf("Expected annotations to be equal but got %v (expected) and %v (actual)", c.expectAnnotations, annotations)
}
})
}
}
36 changes: 36 additions & 0 deletions pkg/addonmanager/controllers/agentdeploy/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agentdeploy

import (
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -292,10 +293,16 @@ func (b *addonWorksBuilder) BuildDeployWorks(addonWorkNamespace string,
}
}

annotations, err := configsToAnnotations(addon.Status.ConfigReferences)
if err != nil {
return nil, nil, err
}

return b.workBuilder.Build(deployObjects,
newAddonWorkObjectMeta(b.processor.manifestWorkNamePrefix(addon.Namespace, addon.Name), addon.Name, addon.Namespace, addonWorkNamespace, owner),
workbuilder.ExistingManifestWorksOption(existingWorks),
workbuilder.ManifestConfigOption(manifestOptions),
workbuilder.ManifestAnnotations(annotations),
workbuilder.DeletionOption(deletionOption))
}

Expand Down Expand Up @@ -492,3 +499,32 @@ func getDeletionOrphaningRule(obj runtime.Object) (*workapiv1.OrphaningRule, err
}
return rule, nil
}

func configsToAnnotations(configReference []addonapiv1alpha1.ConfigReference) (map[string]string, error) {
if len(configReference) == 0 {
return nil, nil
}

annotations := make(map[string]string, len(configReference))
for _, v := range configReference {
if v.DesiredConfig == nil {
continue
}
resourceStr := v.Resource
if len(v.Group) > 0 {
resourceStr += fmt.Sprintf(".%s", v.Group)
}
resourceStr += fmt.Sprintf("/%s/%s", v.DesiredConfig.Namespace, v.DesiredConfig.Name)

annotations[resourceStr] = v.DesiredConfig.SpecHash
}

jsonBytes, err := json.Marshal(annotations)
if err != nil {
return nil, err
}

return map[string]string{
workapiv1.ManifestConfigSpecHashAnnotationKey: string(jsonBytes),
}, nil
}
3 changes: 2 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ k8s.io/utils/path
k8s.io/utils/pointer
k8s.io/utils/strings/slices
k8s.io/utils/trace
# open-cluster-management.io/api v0.10.1-0.20230216050312-b716d4a5c514
# open-cluster-management.io/api v0.10.1-0.20230216050312-b716d4a5c514 => github.com/haoqing0110/api v0.0.0-20230314030136-5f4cd436ed1a
## explicit; go 1.19
open-cluster-management.io/api/addon/v1alpha1
open-cluster-management.io/api/client/addon/clientset/versioned
Expand Down Expand Up @@ -1319,3 +1319,4 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.3.0
## explicit; go 1.12
sigs.k8s.io/yaml
# open-cluster-management.io/api => github.com/haoqing0110/api v0.0.0-20230314030136-5f4cd436ed1a
Loading

0 comments on commit cca7df9

Please sign in to comment.