Skip to content
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 .github/workflows/crdgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ jobs:
run: make -C crd/nodenetworkconfig
- name: Regenerate MultitenantNetworkContainer CRD
run: make -C crd/multitenantnetworkcontainer
- name: Regenerate MultitenantPodNetworkConfig CRD
run: make -C crd/multitenantpodnetworkconfig
- name: Fail if the tree is dirty
run: test -z "$(git status --porcelain)"
19 changes: 19 additions & 0 deletions crd/multitenantpodnetworkconfig/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.DEFAULT_GOAL = all

REPO_ROOT = $(shell git rev-parse --show-toplevel)
TOOLS_DIR = $(REPO_ROOT)/build/tools
TOOLS_BIN_DIR = $(REPO_ROOT)/build/tools/bin
CONTROLLER_GEN = $(TOOLS_BIN_DIR)/controller-gen

all: generate manifests

generate: $(CONTROLLER_GEN)
$(CONTROLLER_GEN) object paths="./..."

.PHONY: manifests
manifests: $(CONTROLLER_GEN)
mkdir -p manifests
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=manifests/

$(CONTROLLER_GEN):
@make -C $(REPO_ROOT) $(CONTROLLER_GEN)
3 changes: 3 additions & 0 deletions crd/multitenantpodnetworkconfig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MultitenantPodNetworkConfig CRDs

MTPNC objects represent the network configuration goal state for a pod running a multitenant networked container and are created and managed by control plane as part of the network configuration, during Pod lifecycle events.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !ignore_uncovered
// +build !ignore_uncovered

// Package v1alpha1 contains API Schema definitions for the acn v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=acn.azure.com
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "acn.azure.com", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build !ignore_uncovered
// +build !ignore_uncovered

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Important: Run "make" to regenerate code after modifying this file

// +kubebuilder:object:root=true

// MultitenantPodNetworkConfig is the Schema for the multitenantpodnetworkconfigs API
// +kubebuilder:resource:scope=Namespaced
// +kubebuilder:resource:shortName=mtpnc
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels=managed=
// +kubebuilder:metadata:labels=owner=
// +kubebuilder:printcolumn:name="PodNetworkInstance",type=string,JSONPath=`.spec.podNetworkInstance`
// +kubebuilder:printcolumn:name="PodName",type=string,JSONPath=`.spec.podName`
// +kubebuilder:printcolumn:name="NCID",type=string,JSONPath=`.status.ncID`
// +kubebuilder:printcolumn:name="PrimaryIP",type=string,JSONPath=`.status.primaryIP`
// +kubebuilder:printcolumn:name="MacAddress",type=string,JSONPath=`.status.macAddress`
// +kubebuilder:printcolumn:name="GatewayIP",type=string,JSONPath=`.status.gatewayIP`
type MultitenantPodNetworkConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec MultitenantPodNetworkConfigSpec `json:"spec,omitempty"`
Status MultitenantPodNetworkConfigStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// MultitenantPodNetworkConfigList contains a list of PodNetworkConfig
type MultitenantPodNetworkConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MultitenantPodNetworkConfig `json:"items"`
}

// MultitenantPodNetworkConfigSpec defines the desired state of PodNetworkConfig
type MultitenantPodNetworkConfigSpec struct {
// name of PNI object from requesting cx pod
PodNetworkInstance string `json:"podNetworkInstance,omitempty"`
// name of the requesting cx pod
PodName string `json:"podName,omitempty"`
}

// MultitenantPodNetworkConfigStatus defines the observed state of PodNetworkConfig
type MultitenantPodNetworkConfigStatus struct {
// network container id
NCID string `json:"ncID,omitempty"`
// ip allocated to the network container
PrimaryIP string `json:"primaryIP,omitempty"`
// maps to the NIC to be injected for the network container
MacAddress string `json:"macAddress,omitempty"`
// Gateway IP
GatewayIP string `json:"gatewayIP,omitempty"`
}

func init() {
SchemeBuilder.Register(&MultitenantPodNetworkConfig{}, &MultitenantPodNetworkConfigList{})
}

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

84 changes: 84 additions & 0 deletions crd/multitenantpodnetworkconfig/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package multitenantpodnetworkconfig

import (
"context"
"reflect"

"github.com/Azure/azure-container-networking/crd"
"github.com/Azure/azure-container-networking/crd/multitenantpodnetworkconfig/api/v1alpha1"
"github.com/pkg/errors"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
typedv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

// Scheme is a runtime scheme containing the client-go scheme and the MultitenantPodNetworkConfig scheme.
var Scheme = runtime.NewScheme()

func init() {
_ = scheme.AddToScheme(Scheme)
_ = v1alpha1.AddToScheme(Scheme)
}

// Installer provides methods to manage the lifecycle of the MultitenantPodNetworkConfig resource definition.
type Installer struct {
cli typedv1.CustomResourceDefinitionInterface
}

func NewInstaller(c *rest.Config) (*Installer, error) {
cli, err := crd.NewCRDClientFromConfig(c)
if err != nil {
return nil, errors.Wrap(err, "failed to init crd client")
}
return &Installer{
cli: cli,
}, nil
}

func (i *Installer) create(ctx context.Context, res *v1.CustomResourceDefinition) (*v1.CustomResourceDefinition, error) {
res, err := i.cli.Create(ctx, res, metav1.CreateOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to create mtpnc crd")
}
return res, nil
}

// Installs the embedded MultitenantPodNetworkConfig CRD definition in the cluster.
func (i *Installer) Install(ctx context.Context) (*v1.CustomResourceDefinition, error) {
mtpnc, err := GetMultitenantPodNetworkConfigs()
if err != nil {
return nil, errors.Wrap(err, "failed to get embedded mtpnc crd")
}
return i.create(ctx, mtpnc)
}

// InstallOrUpdate installs the embedded MultitenantPodNetworkConfig CRD definition in the cluster or updates it if present.
func (i *Installer) InstallOrUpdate(ctx context.Context) (*v1.CustomResourceDefinition, error) {
mtpnc, err := GetMultitenantPodNetworkConfigs()
if err != nil {
return nil, errors.Wrap(err, "failed to get embedded mtpnc crd")
}
current, err := i.create(ctx, mtpnc)
if !apierrors.IsAlreadyExists(err) {
return current, err
}
if current == nil {
current, err = i.cli.Get(ctx, mtpnc.Name, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to get existing mtpnc crd")
}
}
if !reflect.DeepEqual(mtpnc.Spec.Versions, current.Spec.Versions) {
mtpnc.SetResourceVersion(current.GetResourceVersion())
previous := *current
current, err = i.cli.Update(ctx, mtpnc, metav1.UpdateOptions{})
if err != nil {
return &previous, errors.Wrap(err, "failed to update existing mtpnc crd")
}
}
return current, nil
}
24 changes: 24 additions & 0 deletions crd/multitenantpodnetworkconfig/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package multitenantpodnetworkconfig

import (
_ "embed"

"github.com/pkg/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"sigs.k8s.io/yaml"
)

// MultitenantPodNetworkConfigsYAML embeds the CRD YAML for downstream consumers.
//
//go:embed manifests/acn.azure.com_multitenantpodnetworkconfigs.yaml
var MultitenantPodNetworkConfigsYAML []byte

// GetMultitenantPodNetworkConfigs parses the raw []byte MultitenantPodNetworkConfigs in
// to a CustomResourceDefinition and returns it or an unmarshalling error.
func GetMultitenantPodNetworkConfigs() (*apiextensionsv1.CustomResourceDefinition, error) {
multitenantPodNetworkConfigs := &apiextensionsv1.CustomResourceDefinition{}
if err := yaml.Unmarshal(MultitenantPodNetworkConfigsYAML, &multitenantPodNetworkConfigs); err != nil {
return nil, errors.Wrap(err, "error unmarshalling embedded mtpnc")
}
return multitenantPodNetworkConfigs, nil
}
21 changes: 21 additions & 0 deletions crd/multitenantpodnetworkconfig/embed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package multitenantpodnetworkconfig

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

const filename = "manifests/acn.azure.com_multitenantpodnetworkconfigs.yaml"

func TestEmbed(t *testing.T) {
b, err := os.ReadFile(filename)
assert.NoError(t, err)
assert.Equal(t, b, MultitenantPodNetworkConfigsYAML)
}

func TestGetMultitenantPodNetworkConfigs(t *testing.T) {
_, err := GetMultitenantPodNetworkConfigs()
assert.NoError(t, err)
}
Loading