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

[Remove Default Objs 4/4] Update Webhook #624

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
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 {
e0ne marked this conversation as resolved.
Show resolved Hide resolved
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
Loading