Skip to content

Commit

Permalink
Fix workload flatnetwork label not updated
Browse files Browse the repository at this point in the history
  • Loading branch information
STARRY-S committed Sep 4, 2024
1 parent c90bc46 commit 1c3b4eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion pkg/admission/webhook/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func (h *Handler) validateWorkload(ar *admissionv1.AdmissionReview) (bool, error
if err != nil {
return false, fmt.Errorf("deserializeWorkloadReview: %w", err)
}
if workload.PodTemplateAnnotations("k8s.v1.cni.cncf.io/networks") == "" {
if workload.PodTemplateAnnotations("k8s.v1.cni.cncf.io/networks") == "" &&
workload.PodTemplateAnnotations("v1.multus-cni.io/default-network") == "" {
return true, nil
}
if workload.PodTemplateAnnotations(flv1.AnnotationSubnet) == "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/flatnetwork.pandaria.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ const (
LabelWorkloadSelector = "workload.user.cattle.io/workloadselector"
LabelProjectID = "field.cattle.io/projectId"

// Specification for IP/MAC Allocation modes
AllocateModeAuto = "auto"
AllocateModeSpecific = "specific"

// Specification for flatModes
FlatModeIPvlan = "ipvlan"
FlatModeMacvlan = "macvlan"
)
Expand Down
13 changes: 8 additions & 5 deletions pkg/controller/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ func syncWorkload[T Workload](_ string, w T) (T, error) {
return w, nil
}

isFlatNetworkEnabled, labels := getFlatNetworkLabel(w)
isFlatNetworkEnabled, labels := getTemplateFlatNetworkLabel(w)
if !isFlatNetworkEnabled {
logrus.WithFields(fieldsWorkload(w)).
Debugf("skip update workload label as flatnetwork not enabled")
return w, nil
}
o, err := workloadHandler.updateWorkloadLabel(w, labels)
Expand All @@ -85,7 +87,9 @@ func syncWorkload[T Workload](_ string, w T) (T, error) {
return w, err
}

func getFlatNetworkLabel(w metav1.Object) (isFlatNetworkEnabled bool, labels map[string]string) {
func getTemplateFlatNetworkLabel(
w metav1.Object,
) (isFlatNetworkEnabled bool, labels map[string]string) {
m := GetTemplateObjectMeta(w)
if m == nil {
return false, nil
Expand All @@ -99,16 +103,15 @@ func getFlatNetworkLabel(w metav1.Object) (isFlatNetworkEnabled bool, labels map
ipType string
subnet string
)
switch a[flv1.LabelFlatNetworkIPType] {
switch a[flv1.AnnotationIP] {
case flv1.AllocateModeAuto:
ipType = flv1.AllocateModeAuto
isFlatNetworkEnabled = true
case "":
default:
ipType = flv1.AllocateModeSpecific
isFlatNetworkEnabled = true
}
subnet = a[flv1.AnnotationSubnet]
isFlatNetworkEnabled = (ipType != "" && subnet != "")

labels = map[string]string{
flv1.LabelFlatNetworkIPType: ipType,
Expand Down

0 comments on commit 1c3b4eb

Please sign in to comment.