Skip to content

Commit

Permalink
add SSA featuregate test too
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Mar 18, 2024
1 parent fdeec6a commit bc9df51
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
83 changes: 76 additions & 7 deletions test/extended/operators/server_side_apply_examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ var _ = g.Describe("[sig-apimachinery]", func() {
}

g.Describe("server-side-apply should function properly", func() {
g.It("should clear fields when they are no longer being applied", func() {
g.It("should clear fields when they are no longer being applied on CRDs", func() {
ctx := context.Background()
isMicroShift, err := exutil.IsMicroShiftCluster(oc.AdminKubeClient())
o.Expect(err).NotTo(o.HaveOccurred())
if isMicroShift {
g.Skip("microshift lacks the API")
}

// TODO use another API. I might have a version in my cache somewhere using a pod.
_, err := oc.AdminConfigClient().ConfigV1().ClusterOperators().Create(ctx, &configv1.ClusterOperator{
_, err = oc.AdminConfigClient().ConfigV1().ClusterOperators().Create(ctx, &configv1.ClusterOperator{
ObjectMeta: metav1.ObjectMeta{
Name: "test-instance",
},
Expand Down Expand Up @@ -83,14 +87,70 @@ var _ = g.Describe("[sig-apimachinery]", func() {
g.Fail("missing SecondType condition")
}
if containsCondition(currInstance.Status.Conditions, "FirstType") {
g.Fail("has FirstType condition unexectedly")
g.Fail("has FirstType condition unexpectedly")
}
})

g.It("should clear fields when they are no longer being applied in FeatureGates", func() {
ctx := context.Background()
isSelfManagedHA, err := exutil.IsSelfManagedHA(ctx, oc.AdminConfigClient())
o.Expect(err).NotTo(o.HaveOccurred())
isSingleNode, err := exutil.IsSelfManagedHA(ctx, oc.AdminConfigClient())
o.Expect(err).NotTo(o.HaveOccurred())
if !isSelfManagedHA && !isSingleNode {
g.Skip("only SelfManagedHA and SingleNode have mutable FeatureGates")
}

addFirstCondition := applyconfigv1.FeatureGate("cluster").
WithStatus(applyconfigv1.FeatureGateStatus().
WithConditions(
metav1.Condition{
Type: "FirstType",
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: "Dummy",
Message: "No Value",
},
),
)
_, err = oc.AdminConfigClient().ConfigV1().FeatureGates().ApplyStatus(ctx, addFirstCondition, fieldManager)
o.Expect(err).NotTo(o.HaveOccurred())

currInstance, err := oc.AdminConfigClient().ConfigV1().FeatureGates().Get(ctx, "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
if !containsMetaCondition(currInstance.Status.Conditions, "FirstType") {
framework.Logf("got conditions: %v", spew.Sdump(currInstance.Status.Conditions))
g.Fail("missing FirstType condition")
}

addJustSecondCondition := applyconfigv1.FeatureGate("cluster").
WithStatus(applyconfigv1.FeatureGateStatus().
WithConditions(
metav1.Condition{
Type: "SecondType",
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: "Dummy",
Message: "No Value",
},
),
)
_, err = oc.AdminConfigClient().ConfigV1().FeatureGates().ApplyStatus(ctx, addJustSecondCondition, fieldManager)
o.Expect(err).NotTo(o.HaveOccurred())

currInstance, err = oc.AdminConfigClient().ConfigV1().FeatureGates().Get(ctx, "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
if !containsMetaCondition(currInstance.Status.Conditions, "SecondType") {
g.Fail("missing SecondType condition")
}
if containsMetaCondition(currInstance.Status.Conditions, "FirstType") {
g.Fail("has FirstType condition unexpectedly")
}
})

g.It("should clear fields when they are no longer being applied in pods", func() {
g.It("should clear fields when they are no longer being applied in built-in APIs", func() {
ctx := context.Background()

// TODO use another API. I might have a version in my cache somewhere using a pod.
_, err := oc.AdminKubeClient().CoreV1().Pods(oc.Namespace()).Create(ctx, pausePod("test-instance"), metav1.CreateOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
defer oc.AdminKubeClient().CoreV1().Pods(oc.Namespace()).Delete(ctx, "test-instance", metav1.DeleteOptions{})
Expand Down Expand Up @@ -134,7 +194,7 @@ var _ = g.Describe("[sig-apimachinery]", func() {
g.Fail("missing SecondType condition")
}
if containsPodCondition(currInstance.Status.Conditions, "FirstType") {
g.Fail("has FirstType condition unexectedly")
g.Fail("has FirstType condition unexpectedly")
}
})
})
Expand All @@ -149,6 +209,15 @@ func containsCondition(podConditions []configv1.ClusterOperatorStatusCondition,
return false
}

func containsMetaCondition(podConditions []metav1.Condition, name string) bool {
for _, curr := range podConditions {
if string(curr.Type) == name {
return true
}
}
return false
}

func containsPodCondition(podConditions []corev1.PodCondition, name string) bool {
for _, curr := range podConditions {
if string(curr.Type) == name {
Expand Down

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

28 changes: 28 additions & 0 deletions test/extended/util/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
operatorv1 "github.com/openshift/api/operator/v1"
securityv1 "github.com/openshift/api/security/v1"
buildv1clienttyped "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned"
configclient "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
imagev1typedclient "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
"github.com/openshift/library-go/pkg/build/naming"
Expand Down Expand Up @@ -2141,6 +2142,33 @@ func IsNamespaceExist(kubeClient *kubernetes.Clientset, namespace string) (bool,
return true, nil
}

func IsSelfManagedHA(ctx context.Context, configClient clientconfigv1.Interface) (bool, error) {
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, nil
}

return infrastructure.Status.ControlPlaneTopology == configv1.HighlyAvailableTopologyMode, nil
}

func IsSingleNode(ctx context.Context, configClient clientconfigv1.Interface) (bool, error) {
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, nil
}

return infrastructure.Status.ControlPlaneTopology == configv1.SingleReplicaTopologyMode, nil
}

func IsHypershift(ctx context.Context, configClient clientconfigv1.Interface) (bool, error) {
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
if err != nil {
return false, nil
}

return infrastructure.Status.ControlPlaneTopology == configv1.ExternalTopologyMode, nil
}

// IsMicroShiftCluster returns "true" if a cluster is MicroShift,
// "false" otherwise. It needs kube-admin client as input.
func IsMicroShiftCluster(kubeClient k8sclient.Interface) (bool, error) {
Expand Down

0 comments on commit bc9df51

Please sign in to comment.