Skip to content

Commit 0a941ec

Browse files
authored
Merge branch 'master' into ankaggar/ankaggar/PN-CRD
Signed-off-by: aggarwal0009 <[email protected]>
2 parents 3449a1d + c633bf5 commit 0a941ec

12 files changed

+359
-4
lines changed

.github/CODEOWNERS

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# The intention of the CODEOWNERS is that a reviewer can be automatically assigned
2-
# to a PR when it is opened instead of needing to be manually assigned or being left
3-
# unassigned. PRs without assigned Reviewers do not get the prompt attention that
2+
# to a PR when it is opened instead of needing to be manually assigned or being left
3+
# unassigned. PRs without assigned Reviewers do not get the prompt attention that
44
# they should.
55
#
6-
# The CODEOWNERS are not Gatekeepers, just someone who has the domain knowledge to
7-
# review a PR in an area.
6+
# The CODEOWNERS are not Gatekeepers, just someone who has the domain knowledge to
7+
# review a PR in an area.
88
#
99
# Rules are evaluated in this order, and the last match is used for auto-assignment.
1010
* @azure/azure-sdn-members
1111
/.github/ @azure/acn-admins
1212
/cns/ @azure/acn-cns-reviewers
13+
/cni/ @azure/acn-cni-reviewers
1314
/dropgz/ @rbtr @camrynl @paulyufan2 @ashvindeodhar @thatmattlong
1415
/npm/ @azure/acn-npm-reviewers
1516
/zapai/ @rbtr @ZetaoZhuang

.github/workflows/crdgen.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
run: make -C crd/multitenantnetworkcontainer
2929
- name: Regenerate PodNetwork CRD
3030
run: make -C crd/external/podnetwork
31+
- name: Regenerate NodeInfo CRD
32+
run: make -C crd/nodeinfo
3133
- name: Regenerate MultitenantPodNetworkConfig CRD
3234
run: make -C crd/multitenantpodnetworkconfig
3335
- name: Fail if the tree is dirty

crd/nodeinfo/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.DEFAULT_GOAL = all
2+
3+
REPO_ROOT = $(shell git rev-parse --show-toplevel)
4+
TOOLS_DIR = $(REPO_ROOT)/build/tools
5+
TOOLS_BIN_DIR = $(REPO_ROOT)/build/tools/bin
6+
CONTROLLER_GEN = $(TOOLS_BIN_DIR)/controller-gen
7+
8+
all: generate manifests
9+
10+
generate: $(CONTROLLER_GEN)
11+
$(CONTROLLER_GEN) object paths="./..."
12+
13+
.PHONY: manifests
14+
manifests: $(CONTROLLER_GEN)
15+
mkdir -p manifests
16+
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=manifests/
17+
18+
$(CONTROLLER_GEN):
19+
@make -C $(REPO_ROOT) $(CONTROLLER_GEN)

crd/nodeinfo/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# NodeInfo CRDs
2+
3+
This CRD is added to enable VNET multitenancy – which will be watched and managed by the control plane.
4+
5+
NodeInfo objects are created by CNS as part of the node registration flow, and is used to pass any metadata from the VM needed by control plane. E.g.: vmUniqueID etc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build !ignore_uncovered
2+
// +build !ignore_uncovered
3+
4+
// Package v1alpha1 contains API Schema definitions for the acn v1alpha1 API group
5+
// +kubebuilder:object:generate=true
6+
// +groupName=acn.azure.com
7+
package v1alpha1
8+
9+
import (
10+
"k8s.io/apimachinery/pkg/runtime/schema"
11+
"sigs.k8s.io/controller-runtime/pkg/scheme"
12+
)
13+
14+
var (
15+
// GroupVersion is group version used to register these objects
16+
GroupVersion = schema.GroupVersion{Group: "acn.azure.com", Version: "v1alpha1"}
17+
18+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
19+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
20+
21+
// AddToScheme adds the types in this group-version to the given scheme.
22+
AddToScheme = SchemeBuilder.AddToScheme
23+
)

crd/nodeinfo/api/v1alpha1/nodeinfo.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build !ignore_uncovered
2+
// +build !ignore_uncovered
3+
4+
package v1alpha1
5+
6+
import (
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
)
9+
10+
// Important: Run "make" to regenerate code after modifying this file
11+
12+
// +kubebuilder:object:root=true
13+
14+
// NodeInfo is the Schema for the NodeInfo API
15+
// +kubebuilder:resource:scope=Namespaced
16+
// +kubebuilder:resource:shortName=ni
17+
// +kubebuilder:resource:path=nodeinfo
18+
// +kubebuilder:printcolumn:name="VMUniqueID",type=string,priority=0,JSONPath=`.spec.vmUniqueID`
19+
type NodeInfo struct {
20+
metav1.TypeMeta `json:",inline"`
21+
metav1.ObjectMeta `json:"metadata,omitempty"`
22+
23+
Spec NodeInfoSpec `json:"spec,omitempty"`
24+
}
25+
26+
// +kubebuilder:object:root=true
27+
28+
// NodeInfoList contains a list of NodeInfo
29+
type NodeInfoList struct {
30+
metav1.TypeMeta `json:",inline"`
31+
metav1.ListMeta `json:"metadata,omitempty"`
32+
Items []NodeInfo `json:"items"`
33+
}
34+
35+
// NodeInfoSpec defines the desired state of NodeInfo
36+
type NodeInfoSpec struct {
37+
// +kubebuilder:validation:Optional
38+
VMUniqueID string `json:"vmUniqueID,omitempty"`
39+
}
40+
41+
func init() {
42+
SchemeBuilder.Register(&NodeInfo{}, &NodeInfoList{})
43+
}

crd/nodeinfo/api/v1alpha1/zz_generated.deepcopy.go

+83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crd/nodeinfo/client.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package nodeinfo
2+
3+
import (
4+
"context"
5+
"reflect"
6+
7+
"github.com/Azure/azure-container-networking/crd"
8+
"github.com/Azure/azure-container-networking/crd/nodeinfo/api/v1alpha1"
9+
"github.com/pkg/errors"
10+
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11+
typedv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
12+
apierrors "k8s.io/apimachinery/pkg/api/errors"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14+
"k8s.io/apimachinery/pkg/runtime"
15+
"k8s.io/client-go/kubernetes/scheme"
16+
"k8s.io/client-go/rest"
17+
)
18+
19+
// Scheme is a runtime scheme containing the client-go scheme and the NodeInfo scheme.
20+
var Scheme = runtime.NewScheme()
21+
22+
func init() {
23+
_ = scheme.AddToScheme(Scheme)
24+
_ = v1alpha1.AddToScheme(Scheme)
25+
}
26+
27+
// Installer provides methods to manage the lifecycle of the NodeInfo resource definition.
28+
type Installer struct {
29+
cli typedv1.CustomResourceDefinitionInterface
30+
}
31+
32+
func NewInstaller(c *rest.Config) (*Installer, error) {
33+
cli, err := crd.NewCRDClientFromConfig(c)
34+
if err != nil {
35+
return nil, errors.Wrap(err, "failed to init crd client")
36+
}
37+
return &Installer{
38+
cli: cli,
39+
}, nil
40+
}
41+
42+
func (i *Installer) create(ctx context.Context, res *v1.CustomResourceDefinition) (*v1.CustomResourceDefinition, error) {
43+
res, err := i.cli.Create(ctx, res, metav1.CreateOptions{})
44+
if err != nil {
45+
return nil, errors.Wrap(err, "failed to create nodeinfo crd")
46+
}
47+
return res, nil
48+
}
49+
50+
// Install installs the embedded NodeInfo CRD definition in the cluster.
51+
func (i *Installer) Install(ctx context.Context) (*v1.CustomResourceDefinition, error) {
52+
nodeinfo, err := GetNodeInfo()
53+
if err != nil {
54+
return nil, errors.Wrap(err, "failed to get embedded nodeinfo crd")
55+
}
56+
return i.create(ctx, nodeinfo)
57+
}
58+
59+
// InstallOrUpdate installs the embedded NodeInfo CRD definition in the cluster or updates it if present.
60+
func (i *Installer) InstallOrUpdate(ctx context.Context) (*v1.CustomResourceDefinition, error) {
61+
nodeinfo, err := GetNodeInfo()
62+
if err != nil {
63+
return nil, errors.Wrap(err, "failed to get embedded nodeinfo crd")
64+
}
65+
current, err := i.create(ctx, nodeinfo)
66+
if !apierrors.IsAlreadyExists(err) {
67+
return current, err
68+
}
69+
if current == nil {
70+
current, err = i.cli.Get(ctx, nodeinfo.Name, metav1.GetOptions{})
71+
if err != nil {
72+
return nil, errors.Wrap(err, "failed to get existing nodeinfo crd")
73+
}
74+
}
75+
if !reflect.DeepEqual(nodeinfo.Spec.Versions, current.Spec.Versions) {
76+
nodeinfo.SetResourceVersion(current.GetResourceVersion())
77+
previous := *current
78+
current, err = i.cli.Update(ctx, nodeinfo, metav1.UpdateOptions{})
79+
if err != nil {
80+
return &previous, errors.Wrap(err, "failed to update existing nodeinfo crd")
81+
}
82+
}
83+
return current, nil
84+
}

crd/nodeinfo/embed.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package nodeinfo
2+
3+
import (
4+
_ "embed"
5+
6+
"github.com/pkg/errors"
7+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
8+
"sigs.k8s.io/yaml"
9+
)
10+
11+
// NodeInfoYAML embeds the CRD YAML for downstream consumers.
12+
//
13+
//go:embed manifests/acn.azure.com_nodeinfo.yaml
14+
var NodeInfoYAML []byte
15+
16+
// GetNodeInfo parses the raw []byte NodeInfo in
17+
// to a CustomResourceDefinition and returns it or an unmarshalling error.
18+
func GetNodeInfo() (*apiextensionsv1.CustomResourceDefinition, error) {
19+
nodeInfo := &apiextensionsv1.CustomResourceDefinition{}
20+
if err := yaml.Unmarshal(NodeInfoYAML, &nodeInfo); err != nil {
21+
return nil, errors.Wrap(err, "error unmarshalling embedded nodeInfo")
22+
}
23+
return nodeInfo, nil
24+
}

crd/nodeinfo/embed_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package nodeinfo
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
const filename = "manifests/acn.azure.com_nodeinfo.yaml"
11+
12+
func TestEmbed(t *testing.T) {
13+
b, err := os.ReadFile(filename)
14+
assert.NoError(t, err)
15+
assert.Equal(t, b, NodeInfoYAML)
16+
}
17+
18+
func TestGetNodeInfo(t *testing.T) {
19+
_, err := GetNodeInfo()
20+
assert.NoError(t, err)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.12.0
7+
name: nodeinfo.acn.azure.com
8+
spec:
9+
group: acn.azure.com
10+
names:
11+
kind: NodeInfo
12+
listKind: NodeInfoList
13+
plural: nodeinfo
14+
singular: nodeinfo
15+
scope: Namespaced
16+
versions:
17+
- additionalPrinterColumns:
18+
- jsonPath: .spec.vmUniqueID
19+
name: VMUniqueID
20+
type: string
21+
name: v1alpha1
22+
schema:
23+
openAPIV3Schema:
24+
description: NodeInfo is the Schema for the NodeInfo API
25+
properties:
26+
apiVersion:
27+
description: 'APIVersion defines the versioned schema of this representation
28+
of an object. Servers should convert recognized schemas to the latest
29+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
30+
type: string
31+
kind:
32+
description: 'Kind is a string value representing the REST resource this
33+
object represents. Servers may infer this from the endpoint the client
34+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
35+
type: string
36+
metadata:
37+
type: object
38+
spec:
39+
description: NodeInfoSpec defines the desired state of NodeInfo
40+
properties:
41+
vmUniqueID:
42+
type: string
43+
type: object
44+
type: object
45+
served: true
46+
storage: true
47+
subresources: {}

crd/nodeinfo/manifests/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package manifests exists to allow the rendered CRD manifests to be
2+
// packaged in to dependent components.
3+
package manifests

0 commit comments

Comments
 (0)