Skip to content

Commit

Permalink
🐛 Fix a bug in apply resources (#389)
Browse files Browse the repository at this point in the history
* Fix a bug in apply resources

This may impact multiple functions

Signed-off-by: Jian Qiu <jqiu@redhat.com>

* Add an e2e test on init update

Signed-off-by: Jian Qiu <jqiu@redhat.com>

---------

Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 authored Nov 8, 2023
1 parent 205e896 commit d7db0ea
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/helpers/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"embed"
"fmt"

"github.com/jonboulle/clockwork"
"github.com/openshift/library-go/pkg/assets"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -88,6 +89,11 @@ func (r *ResourceReader) applyOneObject(info *resource.Info) error {
helper := resource.NewHelper(info.Client, info.Mapping).
DryRun(r.dryRun)

modified, err := kubectlutil.GetModifiedConfiguration(info.Object, false, unstructured.UnstructuredJSONScheme)
if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving modified configuration from:\n%s\nfor:", info.String()), info.Source, err)
}

if err := info.Get(); err != nil {
if !errors.IsNotFound(err) {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving current configuration of:\n%s\nfrom server for:", info.String()), info.Source, err)
Expand All @@ -105,11 +111,6 @@ func (r *ResourceReader) applyOneObject(info *resource.Info) error {
}
}

modified, err := kubectlutil.GetModifiedConfiguration(info.Object, false, unstructured.UnstructuredJSONScheme)
if err != nil {
return cmdutil.AddSourceToErr(fmt.Sprintf("retrieving modified configuration from:\n%s\nfor:", info.String()), info.Source, err)
}

if !r.dryRun {
patcher := newPatcher(info, helper)
patchBytes, patchedObject, err := patcher.Patch(info.Object, modified, info.Source, info.Namespace, info.Name, r.streams.ErrOut)
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/clusteradm/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/kubectl/pkg/scheme"
addonclientset "open-cluster-management.io/api/client/addon/clientset/versioned"
clusterv1client "open-cluster-management.io/api/client/cluster/clientset/versioned"
operatorclient "open-cluster-management.io/api/client/operator/clientset/versioned"
clusterv1 "open-cluster-management.io/api/cluster/v1"

"testing"
Expand All @@ -30,6 +31,7 @@ var apiExtensionsClient apiextensionsclient.Interface
var dynamicClient dynamic.Interface
var clusterClient clusterv1client.Interface
var addonClient addonclientset.Interface
var operatorClient operatorclient.Interface

func TestE2EClusteradm(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
Expand Down Expand Up @@ -72,6 +74,8 @@ var _ = ginkgo.BeforeSuite(func() {
gomega.Expect(err).NotTo(gomega.HaveOccurred())
addonClient, err = addonclientset.NewForConfig(hubConfig)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
operatorClient, err = operatorclient.NewForConfig(hubConfig)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

err = clusterv1.AddToScheme(scheme.Scheme)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
Expand Down
46 changes: 46 additions & 0 deletions test/e2e/clusteradm/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Contributors to the Open Cluster Management project

package clusteradme2e

import (
"context"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = ginkgo.Describe("test clusteradm with bootstrap token in singleton mode", func() {
ginkgo.BeforeEach(func() {
ginkgo.By("clear e2e environment...")
err := e2e.ClearEnv()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})

ginkgo.Context("init cluster manager", func() {
ginkgo.It("should init multiple times with different flags", func() {
ginkgo.By("init hub with bootstrap token")
err := e2e.Clusteradm().Init(
"--use-bootstrap-token",
"--context", e2e.Cluster().Hub().Context(),
"--bundle-version=latest",
)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")

cm, err := operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(len(cm.Spec.RegistrationConfiguration.FeatureGates)).Should(gomega.Equal(1))

err = e2e.Clusteradm().Init(
"--use-bootstrap-token",
"--context", e2e.Cluster().Hub().Context(),
"--feature-gates=ManagedClusterAutoApproval=true",
"--bundle-version=latest",
)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")
cm, err = operatorClient.OperatorV1().ClusterManagers().Get(context.TODO(), "cluster-manager", metav1.GetOptions{})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(len(cm.Spec.RegistrationConfiguration.FeatureGates)).Should(gomega.Equal(2))
})
})
})

0 comments on commit d7db0ea

Please sign in to comment.