Skip to content

Commit

Permalink
Merge pull request #624 from adrianchiris/webhook-create-only-default…
Browse files Browse the repository at this point in the history
…-config

[Remove Default Objs  4/4] Update Webhook
  • Loading branch information
zeeke authored Feb 22, 2024
2 parents 5fcd26d + b98c804 commit b3b9dfa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pkg/webhook/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func mutateSriovNetworkNodePolicy(cr map[string]interface{}) (*v1.AdmissionRespo
reviewResponse.Allowed = true

name := cr["metadata"].(map[string]interface{})["name"]
// Note(adrianc): the "default" policy is deprecated, we keep this skip below
// in case we encounter it in the cluster.
if name == constants.DefaultPolicyName {
// skip the default policy
return &reviewResponse, nil
Expand Down
14 changes: 5 additions & 9 deletions pkg/webhook/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func validateSriovOperatorConfig(cr *sriovnetworkv1.SriovOperatorConfig, operati
log.Log.V(2).Info("validateSriovOperatorConfig", "object", cr)
var warnings []string

if cr.GetName() != consts.DefaultConfigName {
return false, warnings, fmt.Errorf("only default SriovOperatorConfig is used")
if operation == v1.Delete {
return true, warnings, nil
}

if operation == v1.Delete {
warnings = append(warnings, "default SriovOperatorConfig shouldn't be deleted")
if cr.GetName() != consts.DefaultConfigName || cr.GetNamespace() != vars.Namespace {
return false, warnings, fmt.Errorf("only default SriovOperatorConfig in %s namespace is used", vars.Namespace)
}

if cr.Spec.DisableDrain {
Expand Down Expand Up @@ -96,11 +96,7 @@ func validateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePolicy, o
var warnings []string

if cr.GetName() == consts.DefaultPolicyName && cr.GetNamespace() == os.Getenv("NAMESPACE") {
if operation == v1.Delete {
warnings = append(warnings, "default SriovNetworkNodePolicy shouldn't be deleted")
}

// skip validating default policy
// skip validating (deprecated) default policy
return true, warnings, nil
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/webhook/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

. "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"

fakesnclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned/fake"
)
Expand Down Expand Up @@ -136,7 +137,8 @@ func NewNode() *corev1.Node {
func newDefaultOperatorConfig() *SriovOperatorConfig {
return &SriovOperatorConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Name: "default",
Namespace: vars.Namespace,
},
Spec: SriovOperatorConfigSpec{
ConfigDaemonNodeSelector: map[string]string{},
Expand All @@ -157,7 +159,7 @@ func TestValidateSriovOperatorConfigWithDefaultOperatorConfig(t *testing.T) {
ok, w, err := validateSriovOperatorConfig(config, "DELETE")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(ok).To(Equal(true))
g.Expect(w[0]).To(ContainSubstring("default SriovOperatorConfig shouldn't be deleted"))
g.Expect(w).To(BeEmpty())

ok, _, err = validateSriovOperatorConfig(config, "UPDATE")
g.Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -226,7 +228,7 @@ func TestValidateSriovNetworkNodePolicyWithDefaultPolicy(t *testing.T) {
ok, w, err := validateSriovNetworkNodePolicy(policy, "DELETE")
g.Expect(err).NotTo(HaveOccurred())
g.Expect(ok).To(Equal(true))
g.Expect(w[0]).To(ContainSubstring("default SriovNetworkNodePolicy shouldn't be deleted"))
g.Expect(w).To(BeEmpty())

ok, _, err = validateSriovNetworkNodePolicy(policy, "UPDATE")
g.Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit b3b9dfa

Please sign in to comment.