Skip to content

Commit

Permalink
Replace network annotation function with client library function
Browse files Browse the repository at this point in the history
This change remove network annotation function and use
net-attach-def library function. As a result, it is independent
from multus.

This also removes unnecessary files in deployments.
  • Loading branch information
s1061123 committed Oct 17, 2022
1 parent 20df73c commit be9319f
Show file tree
Hide file tree
Showing 20 changed files with 4 additions and 2,986 deletions.
78 changes: 0 additions & 78 deletions deployments/test.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/onsi/gomega v1.10.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
gopkg.in/k8snetworkplumbingwg/multus-cni.v3 v3.7.3-0.20220621194709-ca8c9c579100
k8s.io/api v0.22.8
k8s.io/apimachinery v0.22.8
k8s.io/client-go v0.22.8
Expand Down Expand Up @@ -47,7 +46,6 @@ require (
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
439 changes: 0 additions & 439 deletions go.sum

Large diffs are not rendered by default.

85 changes: 2 additions & 83 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ package controller

import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"regexp"
"sort"
"strings"
"syscall"
"time"

"github.com/containernetworking/cni/libcni"
"github.com/golang/glog"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/logging"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/types"
"github.com/k8snetworkplumbingwg/net-attach-def-admission-controller/pkg/localmetrics"
netv1_utils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
netattachdefClientset "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
api_v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -370,7 +367,7 @@ func (c *Controller) updateMetrics(key string, configNames string, namespace str
c.updateMetrics(key, "", namespace, Delete)
}

networks, err := c.parsePodNetworkAnnotation(configNames, namespace)
networks, err := netv1_utils.ParseNetworkAnnotation(configNames, namespace)
if err != nil {
return fmt.Errorf("Error reading pod annotation %v", err)
}
Expand Down Expand Up @@ -406,81 +403,3 @@ func (c *Controller) updateMetrics(key string, configNames string, namespace str
return nil

}

func (c *Controller) parsePodNetworkAnnotation(podNetworks, defaultNamespace string) ([]*types.NetworkSelectionElement, error) {
var networks []*types.NetworkSelectionElement

if podNetworks == "" {
return nil, fmt.Errorf("parsePodNetworkAnnotation: pod annotation not having \"network\" as key, refer Multus README.md for the usage guide")
}

if strings.IndexAny(podNetworks, "[{\"") >= 0 {
if err := json.Unmarshal([]byte(podNetworks), &networks); err != nil {
return nil, fmt.Errorf("parsePodNetworkAnnotation: failed to parse pod Network Attachment Selection Annotation JSON format: %v", err)
}
} else {
// Comma-delimited list of network attachment object names
for _, item := range strings.Split(podNetworks, ",") {
// Remove leading and trailing whitespace.
item = strings.TrimSpace(item)

// Parse network name (i.e. <namespace>/<network name>@<ifname>)
netNsName, networkName, netIfName, err := c.parsePodNetworkObjectName(item)
if err != nil {
return nil, fmt.Errorf("parsePodNetworkAnnotation: %v", err)
}

networks = append(networks, &types.NetworkSelectionElement{
Name: networkName,
Namespace: netNsName,
InterfaceRequest: netIfName,
})
}
}

for _, net := range networks {
if net.Namespace == "" {
net.Namespace = defaultNamespace
}
}
return networks, nil
}

func (c *Controller) parsePodNetworkObjectName(podnetwork string) (string, string, string, error) {
var netNsName string
var netIfName string
var networkName string

slashItems := strings.Split(podnetwork, "/")
if len(slashItems) == 2 {
netNsName = strings.TrimSpace(slashItems[0])
networkName = slashItems[1]
} else if len(slashItems) == 1 {
networkName = slashItems[0]
} else {
return "", "", "", fmt.Errorf("parsePodNetworkObjectName: Invalid network object (failed at '/')")
}

atItems := strings.Split(networkName, "@")
networkName = strings.TrimSpace(atItems[0])
if len(atItems) == 2 {
netIfName = strings.TrimSpace(atItems[1])
} else if len(atItems) != 1 {
return "", "", "", fmt.Errorf("parsePodNetworkObjectName: Invalid network object (failed at '@')")
}

// Check and see if each item matches the specification for valid attachment name.
// "Valid attachment names must be comprised of units of the DNS-1123 label format"
// [a-z0-9]([-a-z0-9]*[a-z0-9])?
// And we allow at (@), and forward slash (/) (units separated by commas)
// It must start and end alphanumerically.
allItems := []string{netNsName, networkName, netIfName}
for i := range allItems {
matched, _ := regexp.MatchString("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", allItems[i])
if !matched && len([]rune(allItems[i])) > 0 {
return "", "", "", logging.Errorf(fmt.Sprintf("parsePodNetworkObjectName: Failed to parse: one or more items did not match comma-delimited format (must consist of lower case alphanumeric characters). Must start and end with an alphanumeric character), mismatch @ '%v'", allItems[i]))
}
}

return netNsName, networkName, netIfName, nil
}
87 changes: 2 additions & 85 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"net/http"
"os"
"regexp"
"strings"

"github.com/containernetworking/cni/libcni"
"github.com/golang/glog"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/types"
netv1_utils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
netv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/pkg/errors"
"k8s.io/api/admission/v1beta1"
Expand Down Expand Up @@ -243,7 +242,7 @@ func analyzeIsolationAnnotation(ar *v1beta1.AdmissionReview) (bool, error) {

glog.Infof("Analyzing %s annotation: %s", networksAnnotationKey, annotations[networksAnnotationKey])

networks, err := parsePodNetworkAnnotation(annotations[networksAnnotationKey], namespaceConstraint)
networks, err := netv1_utils.ParseNetworkAnnotation(annotations[networksAnnotationKey], namespaceConstraint)
if err != nil {
glog.Errorf("Error during parsePodNetworkAnnotation: %v", err)
return false, err
Expand All @@ -266,88 +265,6 @@ func analyzeIsolationAnnotation(ar *v1beta1.AdmissionReview) (bool, error) {

}

func parsePodNetworkAnnotation(podNetworks, defaultNamespace string) ([]*types.NetworkSelectionElement, error) {
var networks []*types.NetworkSelectionElement

// logging.Debugf("parsePodNetworkAnnotation: %s, %s", podNetworks, defaultNamespace)
if podNetworks == "" {
return nil, fmt.Errorf("parsePodNetworkAnnotation: pod annotation not having \"network\" as key, refer Multus README.md for the usage guide")
}

if strings.IndexAny(podNetworks, "[{\"") >= 0 {
if err := json.Unmarshal([]byte(podNetworks), &networks); err != nil {
return nil, fmt.Errorf("parsePodNetworkAnnotation: failed to parse pod Network Attachment Selection Annotation JSON format: %v", err)
}
} else {
// Comma-delimited list of network attachment object names
for _, item := range strings.Split(podNetworks, ",") {
// Remove leading and trailing whitespace.
item = strings.TrimSpace(item)

// Parse network name (i.e. <namespace>/<network name>@<ifname>)
netNsName, networkName, netIfName, err := parsePodNetworkObjectName(item)
if err != nil {
return nil, fmt.Errorf("parsePodNetworkAnnotation: %v", err)
}

networks = append(networks, &types.NetworkSelectionElement{
Name: networkName,
Namespace: netNsName,
InterfaceRequest: netIfName,
})
}
}

for _, net := range networks {
if net.Namespace == "" {
net.Namespace = defaultNamespace
}
}

return networks, nil
}

func parsePodNetworkObjectName(podnetwork string) (string, string, string, error) {
var netNsName string
var netIfName string
var networkName string

// logging.Debugf("parsePodNetworkObjectName: %s", podnetwork)
slashItems := strings.Split(podnetwork, "/")
if len(slashItems) == 2 {
netNsName = strings.TrimSpace(slashItems[0])
networkName = slashItems[1]
} else if len(slashItems) == 1 {
networkName = slashItems[0]
} else {
return "", "", "", fmt.Errorf("Invalid network object (failed at '/')")
}

atItems := strings.Split(networkName, "@")
networkName = strings.TrimSpace(atItems[0])
if len(atItems) == 2 {
netIfName = strings.TrimSpace(atItems[1])
} else if len(atItems) != 1 {
return "", "", "", fmt.Errorf("Invalid network object (failed at '@')")
}

// Check and see if each item matches the specification for valid attachment name.
// "Valid attachment names must be comprised of units of the DNS-1123 label format"
// [a-z0-9]([-a-z0-9]*[a-z0-9])?
// And we allow at (@), and forward slash (/) (units separated by commas)
// It must start and end alphanumerically.
allItems := []string{netNsName, networkName, netIfName}
for i := range allItems {
matched, _ := regexp.MatchString("^[a-z0-9]([-a-z0-9]*[a-z0-9])?$", allItems[i])
if !matched && len([]rune(allItems[i])) > 0 {
return "", "", "", fmt.Errorf(fmt.Sprintf("Failed to parse: one or more items did not match comma-delimited format (must consist of lower case alphanumeric characters). Must start and end with an alphanumeric character), mismatch @ '%v'", allItems[i]))
}
}

// logging.Debugf("parsePodNetworkObjectName: parsed: %s, %s, %s", netNsName, networkName, netIfName)
return netNsName, networkName, netIfName, nil
}

func deserializeNetworkAttachmentDefinition(ar *v1beta1.AdmissionReview) (netv1.NetworkAttachmentDefinition, error) {
/* unmarshal NetworkAttachmentDefinition from AdmissionReview request */
netAttachDef := netv1.NetworkAttachmentDefinition{}
Expand Down
Loading

0 comments on commit be9319f

Please sign in to comment.