Skip to content

Commit

Permalink
Revise antctl mc deploy to support manifest update (#5257)
Browse files Browse the repository at this point in the history
If a resource to create already exists, the command `antctl mc deploy`
will do nothing even when the manifests are changed. Revise the code to
apply changes if there is any manifest update.

Signed-off-by: Lan Luo <luola@vmware.com>
  • Loading branch information
luolanzone committed Nov 7, 2023
1 parent 0196e18 commit 6eeb593
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
16 changes: 15 additions & 1 deletion pkg/antctl/raw/multicluster/deploy/deploy_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,21 @@ func createResources(cmd *cobra.Command, apiGroupResources []*restmapper.APIGrou
if !kerrors.IsAlreadyExists(err) {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s/%s already exists\n", unstructuredObj.GetKind(), unstructuredObj.GetName())
existingRes, err := dri.Get(context.TODO(), unstructuredObj.GetName(), metav1.GetOptions{})
if err != nil {
return err
}
existingVersion := existingRes.GetResourceVersion()
unstructuredObj.SetResourceVersion(existingVersion)
var updatedObj *unstructured.Unstructured
if updatedObj, err = dri.Update(context.TODO(), unstructuredObj, metav1.UpdateOptions{}); err != nil {
return err
}
if updatedObj.GetResourceVersion() != existingVersion {
fmt.Fprintf(cmd.OutOrStdout(), "%s/%s configured\n", unstructuredObj.GetKind(), unstructuredObj.GetName())
} else {
fmt.Fprintf(cmd.OutOrStdout(), "%s/%s unchanged\n", unstructuredObj.GetKind(), unstructuredObj.GetName())
}
} else {
fmt.Fprintf(cmd.OutOrStdout(), "%s/%s created\n", unstructuredObj.GetKind(), unstructuredObj.GetName())
}
Expand Down
67 changes: 62 additions & 5 deletions pkg/antctl/raw/multicluster/deploy/deploy_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package deploy

import (
"bytes"
"errors"
"io"
"log"
Expand All @@ -27,10 +28,18 @@ import (

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
fakedisco "k8s.io/client-go/discovery/fake"
dynamicfake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/restmapper"
coretesting "k8s.io/client-go/testing"

mcscheme "antrea.io/antrea/pkg/antctl/raw/multicluster/scheme"
)
Expand Down Expand Up @@ -97,18 +106,66 @@ func TestGenerateManifests(t *testing.T) {

func TestCreateResources(t *testing.T) {
fakeDynamicClient := dynamicfake.NewSimpleDynamicClient(mcscheme.Scheme)
fakeClient := fake.NewSimpleClientset()
apiGroupResources, _ := restmapper.GetAPIGroupResources(fakeClient.Discovery())
fakeDynamicClient.PrependReactor("create", "customresourcedefinitions", func(action coretesting.Action) (bool, runtime.Object, error) {
crd := action.(coretesting.CreateAction).GetObject().(*unstructured.Unstructured)
metadata, ok := crd.UnstructuredContent()["metadata"].(map[string]interface{})
if ok && metadata["name"] == "clustersets.multicluster.crd.antrea.io" {
return true, nil, k8serrors.NewAlreadyExists(schema.GroupResource{Resource: "customresourcedefinitions"}, "clustersets")
}
return true, nil, nil
})
fakeDynamicClient.PrependReactor("get", "customresourcedefinitions", func(action coretesting.Action) (bool, runtime.Object, error) {
if action.(coretesting.GetAction).GetName() == "clustersets.multicluster.crd.antrea.io" {
return true, &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "customresourcedefinitions",
"metadata": map[string]interface{}{
"name": "clustersets.multicluster.crd.antrea.io",
},
},
}, nil
}
return true, nil, nil
})
fakeDynamicClient.PrependReactor("update", "customresourcedefinitions", func(action coretesting.Action) (bool, runtime.Object, error) {
crd := action.(coretesting.UpdateAction).GetObject().(*unstructured.Unstructured)
metadata, ok := crd.UnstructuredContent()["metadata"].(map[string]interface{})
if ok && metadata["name"] == "clustersets.multicluster.crd.antrea.io" {
return true, &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "customresourcedefinitions",
"metadata": map[string]interface{}{
"name": "clustersets.multicluster.crd.antrea.io",
"resourceVersion": "100",
},
},
}, nil
}
return true, nil, nil
})
fakeDiscoveryClient := &fakedisco.FakeDiscovery{Fake: &coretesting.Fake{}}
fakeDiscoveryClient.Resources = []*metav1.APIResourceList{
{
GroupVersion: apiextensionsv1.SchemeGroupVersion.String(),
APIResources: []metav1.APIResource{
{Name: "customresourcedefinitions", Namespaced: false, Kind: "CustomResourceDefinition"},
},
},
}
apiGroupResources, _ := restmapper.GetAPIGroupResources(fakeDiscoveryClient)
cmd := &cobra.Command{}
buf := new(bytes.Buffer)
cmd.SetOutput(buf)
file := filepath.Join("..", "..", "..", "..", "..", "multicluster", "build", "yamls", "antrea-multicluster-leader-global.yml")
content, err := os.ReadFile(file)
if err != nil {
t.Errorf("Failed to open the file %s", file)
}
err = createResources(cmd, apiGroupResources, fakeDynamicClient, content)
if err != nil {
assert.Contains(t, err.Error(), "no matches for kind \"CustomResourceDefinition\"")
}
assert.NoError(t, err)
assert.Contains(t, buf.String(), "CustomResourceDefinition/clustersets.multicluster.crd.antrea.io configured\n")
}

func TestDeploy(t *testing.T) {
Expand Down

0 comments on commit 6eeb593

Please sign in to comment.