Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix a bug in apply resources #389

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @qiujian16 could you add some comments/tests for this, I do not understand why moving this func upper can fix the issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to get the modified object, it has to be called before info.Get() otherwise the info.Object will be from the server. A test is added.

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))
})
})
})
Loading