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

e2e test taking ownership of a field #28740

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ssawithsets.testing.openshift.io
spec:
group: testing.openshift.io
names:
kind: SSAWithSet
listKind: ISSAWithSetList
plural: ssawithsets
singular: ssawithset
scope: Cluster
versions:
- name: v1
schema:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
properties:
unrelatedField:
type: string
setList:
items:
maxLength: 43
minLength: 1
type: string
maxItems: 32
type: array
x-kubernetes-list-type: set
type: object
required:
- spec
type: object
served: true
storage: true
subresources:
status: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: testing.openshift.io/v1
kind: SSAWithSet
metadata:
name: foo
spec:
unrelatedField: keep-original-owner
setList:
- apple
- banana
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: testing.openshift.io/v1
kind: SSAWithSet
metadata:
name: foo
spec:
unrelatedField: keep-original-owner
setList:
- first
- second
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: testing.openshift.io/v1
kind: SSAWithSet
metadata:
name: foo
spec:
setList:
- first
- second
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: testing.openshift.io/v1
kind: SSAWithSet
metadata:
name: foo
spec:
setList:
- apple
- banana
97 changes: 97 additions & 0 deletions test/extended/operators/server_side_apply_examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package operators

import (
"context"
_ "embed"
"encoding/json"
"github.com/openshift/library-go/pkg/operator/resource/resourceread"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"time"

applycorev1 "k8s.io/client-go/applyconfigurations/core/v1"

Expand All @@ -22,6 +31,33 @@ import (
admissionapi "k8s.io/pod-security-admission/api"
)

var (
//go:embed manifests/ssa-with-set/crd-with-ssa-set.yaml
ssaWithSet []byte
//go:embed manifests/ssa-with-set/new-instance.yaml
ssaWithSetNewInstance []byte
//go:embed manifests/ssa-with-set/take-ownership-instance.yaml
ssaWithSetTakeOwnership []byte
//go:embed manifests/ssa-with-set/updated-list.yaml
ssaWithSetUpdatedList []byte
//go:embed manifests/ssa-with-set/expected-final.yaml
ssaWithSetExpectedFinal []byte

ssaWithSetCRD *apiextensionsv1.CustomResourceDefinition
ssaWithSetNewInstanceObj *unstructured.Unstructured
ssaWithSetTakeOwnershipObj *unstructured.Unstructured
ssaWithSetUpdatedListObj *unstructured.Unstructured
ssaWithSetExpectedFinalObj *unstructured.Unstructured
)

func init() {
ssaWithSetCRD = resourceread.ReadCustomResourceDefinitionV1OrDie(ssaWithSet)
ssaWithSetNewInstanceObj = resourceread.ReadUnstructuredOrDie(ssaWithSetNewInstance)
ssaWithSetTakeOwnershipObj = resourceread.ReadUnstructuredOrDie(ssaWithSetTakeOwnership)
ssaWithSetUpdatedListObj = resourceread.ReadUnstructuredOrDie(ssaWithSetUpdatedList)
ssaWithSetExpectedFinalObj = resourceread.ReadUnstructuredOrDie(ssaWithSetExpectedFinal)
}

var _ = g.Describe("[sig-apimachinery]", func() {

defer g.GinkgoRecover()
Expand All @@ -32,6 +68,67 @@ var _ = g.Describe("[sig-apimachinery]", func() {
}

g.Describe("server-side-apply should function properly", func() {
g.It("should take ownership of a list set", func() {
ctx := context.Background()

crdClient, err := apiextensionsclientset.NewForConfig(oc.AdminConfig())
o.Expect(err).NotTo(o.HaveOccurred())
_, err = crdClient.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, ssaWithSetCRD, metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
defer func() {
err := crdClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.TODO(), ssaWithSetCRD.Name, metav1.DeleteOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
}()

dynamicClient := oc.AdminDynamicClient()
ssaClient := dynamicClient.Resource(schema.GroupVersionResource{
Group: "testing.openshift.io",
Version: "v1",
Resource: "ssawithsets",
})

err = wait.PollUntilContextTimeout(ctx, 1*time.Second, 30*time.Second, true,
func(ctx context.Context) (bool, error) {
_, err := ssaClient.Apply(ctx, ssaWithSetNewInstanceObj.GetName(), ssaWithSetNewInstanceObj, metav1.ApplyOptions{
FieldManager: "creator",
})
if err == nil {
return true, nil
}
if err != nil {
framework.Logf("failed to create: %v", err)
}

return false, nil
})
o.Expect(err).NotTo(o.HaveOccurred())

postApply, err := ssaClient.Apply(ctx, ssaWithSetTakeOwnershipObj.GetName(), ssaWithSetTakeOwnershipObj, metav1.ApplyOptions{
FieldManager: "new-owner",
Force: true,
})
postBytes, _ := json.Marshal(postApply.Object)
framework.Logf("after sharing the field\n%v", string(postBytes))
o.Expect(err).NotTo(o.HaveOccurred())

postApply, err = ssaClient.Apply(ctx, ssaWithSetUpdatedListObj.GetName(), ssaWithSetUpdatedListObj, metav1.ApplyOptions{
FieldManager: "new-owner",
deads2k marked this conversation as resolved.
Show resolved Hide resolved
Force: true,
})
postBytes, _ = json.Marshal(postApply.Object)
framework.Logf("after trying to replace the field\n%v", string(postBytes))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

{
  "apiVersion": "testing.openshift.io/v1",
  "kind": "SSAWithSet",
  "metadata": {
    "creationTimestamp": "2024-04-24T21:14:18Z",
    "generation": 2,
    "managedFields": [
      {
        "apiVersion": "testing.openshift.io/v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:spec": {
            "f:setList": {
              "v:\"first\"": {},
              "v:\"second\"": {}
            },
            "f:unrelatedField": {}
          }
        },
        "manager": "creator",
        "operation": "Apply",
        "time": "2024-04-24T21:14:18Z"
      },
      {
        "apiVersion": "testing.openshift.io/v1",
        "fieldsType": "FieldsV1",
        "fieldsV1": {
          "f:spec": {
            "f:setList": {
              "v:\"apple\"": {},
              "v:\"banana\"": {}
            }
          }
        },
        "manager": "new-owner",
        "operation": "Apply",
        "time": "2024-04-24T21:14:18Z"
      }
    ],
    "name": "foo",
    "resourceVersion": "65295",
    "uid": "27acfc2e-7452-4722-b043-5e1a86cab35f"
  },
  "spec": {
    "setList": [
      "first",
      "second",
      "apple",
      "banana"
    ],
    "unrelatedField": "keep-original-owner"
  }
}

o.Expect(err).NotTo(o.HaveOccurred())

actualFinal, err := ssaClient.Get(ctx, ssaWithSetUpdatedListObj.GetName(), metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())

actualSpec, _, err := unstructured.NestedMap(actualFinal.Object, "spec")
o.Expect(err).NotTo(o.HaveOccurred())
expectedSpec, _, err := unstructured.NestedMap(ssaWithSetExpectedFinalObj.Object, "spec")
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(actualSpec).To(o.Equal(expectedSpec))
})

g.It("should clear fields when they are no longer being applied on CRDs", func() {
ctx := context.Background()
isMicroShift, err := exutil.IsMicroShiftCluster(oc.AdminKubeClient())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.