Skip to content

Commit 2b9dead

Browse files
sindhusri16joekrPeizhao Li
authored
Feat: VCN Native CNI Changes (#407)
* bug: Update the CRDs for the workrequest feature (#387) This was left out of the previous feature add. * changes to uptake go-v1.22.9 * manifests generated with new controller gen * Feat: API Spec & CRD & Conversion for VCN Native Params * Feat: NPN CR Reconciliation in OCIMachine_controller * Fix: Add common label for compatibility & remove non-related crd changes from open source * Revert "Fix: Add common label for compatibility & remove non-related crd changes from open source" This reverts commit f816852. * Fix: Improve NPN CR Reconcile * Fix: Kube Client Creation * Fix: Update CNI Type String * Fix: Return nil upon retry * Fix: unused var * Fix: improve logging and error display * Fix: Use different CR for get and create * Feat: include the NPN Reconcile into client scope package * Style: Remove Redudant Const * Feat: Move CniType to OCICluster * Image build * Image build * Image build * Logging update --------- Co-authored-by: Joe Kratzat <[email protected]> Co-authored-by: Peizhao Li <[email protected]>
1 parent a0ced3d commit 2b9dead

28 files changed

+5421
-6218
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.21.8 as builder
2+
FROM golang:1.22.9 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ envsubst: $(ENVSUBST) ## Build a local copy of envsubst.
369369
kubectl: $(KUBECTL) ## Build a local copy of kubectl.
370370

371371
$(CONTROLLER_GEN): ## Download controller-gen locally if necessary.
372-
GOBIN=$(BIN_DIR)/ $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) v0.13.0
372+
GOBIN=$(BIN_DIR)/ $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) v0.14.0
373373

374374
$(CONVERSION_GEN): ## Download controller-gen locally if necessary.
375375
GOBIN=$(BIN_DIR)/ $(GO_INSTALL) k8s.io/code-generator/cmd/conversion-gen $(CONVERSION_GEN_BIN) v0.23.1

api/v1beta1/ocicluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func (src *OCICluster) ConvertTo(dstRaw conversion.Hub) error {
4848
dst.Spec.NetworkSpec.Vcn.InternetGateway.Skip = restored.Spec.NetworkSpec.Vcn.InternetGateway.Skip
4949
dst.Spec.NetworkSpec.Vcn.RouteTable.Skip = restored.Spec.NetworkSpec.Vcn.RouteTable.Skip
5050
dst.Spec.NetworkSpec.APIServerLB.LoadBalancerType = restored.Spec.NetworkSpec.APIServerLB.LoadBalancerType
51+
dst.Spec.NetworkSpec.CniType = restored.Spec.NetworkSpec.CniType
5152
dst.Spec.ClientOverrides = restored.Spec.ClientOverrides
5253

5354
return nil

api/v1beta1/ocimachine_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ type OCIMachineSpec struct {
145145
// Specifies whether to delete or preserve the data volumes created during launch when
146146
//terminating an instance. When set to true, the data volumes are preserved. The default value is true.
147147
PreserveDataVolumesCreatedAtLaunch bool `json:"preserveDataVolumesCreatedAtLaunch,omitempty"`
148+
149+
// Specifies the list of pod subnets being used for the VCN IP NATIVE CNI type for pod networking.
150+
// Set on each NPN CR associated with the OCI Machine.
151+
PodSubnetIds []string `json:"podSubnetIds,omitempty"`
152+
153+
// Specifies the maximum number of pods allowed for each node, decided by the shape
154+
MaxPodPerNode int `json:"maxPodCount,omitempty"`
155+
156+
// Specifies the list of Network Security Groups used for the VCN IP NATIVE CNI type for pod networking.
157+
// Set on each NPN CR associated with the OCI Machine.
158+
PodNSGIds []string `json:"podNsgIds,omitempty"`
148159
}
149160

150161
// OCIMachineStatus defines the observed state of OCIMachine.

api/v1beta1/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,9 @@ type NetworkSpec struct {
10091009
// VCNPeering configuration.
10101010
// +optional
10111011
VCNPeering *VCNPeering `json:"vcnPeering,omitempty"`
1012+
1013+
// The CNI to be used are OCI_VCN_IP_NATIVE and FLANNEL_OVERLAY
1014+
CniType CNIOptionEnum `json:"cniType,omitempty"`
10121015
}
10131016

10141017
// VCNPeering defines the VCN peering details of the workload cluster VCN.

api/v1beta1/zz_generated.conversion.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/ocimachine_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ type OCIMachineSpec struct {
139139
// Specifies whether to delete or preserve the data volumes created during launch when
140140
//terminating an instance. When set to true, the data volumes are preserved. The default value is true.
141141
PreserveDataVolumesCreatedAtLaunch bool `json:"preserveDataVolumesCreatedAtLaunch,omitempty"`
142+
143+
// Specifies the list of pod subnets being used for the VCN IP NATIVE CNI type for pod networking.
144+
// Set on each NPN CR associated with the OCI Machine.
145+
PodSubnetIds []string `json:"podSubnetIds,omitempty"`
146+
147+
// Specifies the maximum number of pods allowed for each node, decided by the shape
148+
MaxPodPerNode int `json:"maxPodCount,omitempty"`
149+
150+
// Specifies the list of Network Security Groups used for the VCN IP NATIVE CNI type for pod networking.
151+
// Set on each NPN CR associated with the OCI Machine.
152+
PodNSGIds []string `json:"podNsgIds,omitempty"`
142153
}
143154

144155
// OCIMachineStatus defines the observed state of OCIMachine.

api/v1beta2/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,9 @@ type NetworkSpec struct {
10181018
// VCNPeering configuration.
10191019
// +optional
10201020
VCNPeering *VCNPeering `json:"vcnPeering,omitempty"`
1021+
1022+
// The CNI to be used are OCI_VCN_IP_NATIVE and FLANNEL_OVERLAY
1023+
CniType CNIOptionEnum `json:"cniType,omitempty"`
10211024
}
10221025

10231026
// VCNPeering defines the VCN peering details of the workload cluster VCN.

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
Copyright (c) 2022 Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scope
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strings"
23+
24+
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
27+
"k8s.io/client-go/tools/clientcmd"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
)
30+
31+
const (
32+
npnVersion = "oci.oraclecloud.com/v1beta1"
33+
npnKind = "NativePodNetwork"
34+
apiExtensionVersion = "apiextensions.k8s.io/v1"
35+
npnCrdName = "nativepodnetworks.oci.oraclecloud.com"
36+
)
37+
38+
func (m *MachineScope) NewWorkloadClient(ctx context.Context) (wlClient client.Client, err error) {
39+
u := &unstructured.Unstructured{}
40+
u.SetGroupVersionKind(schema.GroupVersionKind{
41+
Kind: "Secret",
42+
Version: "v1",
43+
})
44+
cluster := m.Cluster
45+
46+
secret_obj := client.ObjectKey{
47+
Namespace: cluster.Namespace,
48+
Name: cluster.Spec.InfrastructureRef.Name + "-kubeconfig",
49+
}
50+
secret := &corev1.Secret{}
51+
if err := m.Client.Get(ctx, secret_obj, secret); err != nil {
52+
return nil, err
53+
}
54+
secretData := secret.Data["value"]
55+
config, err := clientcmd.RESTConfigFromKubeConfig(secretData)
56+
if err != nil {
57+
m.Info(fmt.Sprintf("error build config: %s", err))
58+
return nil, err
59+
}
60+
61+
wlClient, err = client.New(config, client.Options{})
62+
63+
return wlClient, err
64+
}
65+
66+
func (m *MachineScope) DeleteNpn(ctx context.Context) error {
67+
m.Info("DELETE NPN CR NOW.")
68+
69+
wlClient, err := m.NewWorkloadClient(ctx)
70+
if err != nil {
71+
m.Info(fmt.Sprintf("Failed to initialize kube client set: %s", err))
72+
return err
73+
}
74+
instance, err := m.GetOrCreateMachine(ctx)
75+
if err != nil {
76+
m.Info(fmt.Sprintf("Failed to get machine: %s", err))
77+
return err
78+
}
79+
npnCr := &unstructured.Unstructured{}
80+
slicedId := strings.Split(*instance.Id, ".")
81+
instanceSuffix := slicedId[len(slicedId)-1]
82+
npnCr.SetName(instanceSuffix)
83+
npnCr.SetGroupVersionKind(schema.GroupVersionKind{
84+
Version: npnVersion,
85+
Kind: npnKind,
86+
})
87+
if err := wlClient.Delete(ctx, npnCr); err != nil {
88+
m.Info(fmt.Sprintf("Failed to delete NPN CR: %s", err))
89+
return err
90+
}
91+
return nil
92+
}
93+
94+
func (m *MachineScope) HasNpnCrd(ctx context.Context) (bool, error) {
95+
m.Info("Get NPN CRD Now.")
96+
97+
wlClient, err := m.NewWorkloadClient(ctx)
98+
if err != nil {
99+
return false, err
100+
}
101+
npnCrd := &unstructured.Unstructured{}
102+
npnCrd.SetGroupVersionKind(schema.GroupVersionKind{
103+
Version: apiExtensionVersion,
104+
Kind: "CustomResourceDefinition",
105+
})
106+
107+
err = wlClient.Get(context.Background(), client.ObjectKey{
108+
Name: npnCrdName,
109+
}, npnCrd)
110+
if err != nil {
111+
m.Info(fmt.Sprintf("Failed to Get NPN CRD, reason: %v", err))
112+
return false, err
113+
}
114+
115+
return true, nil
116+
117+
}
118+
119+
func (m *MachineScope) GetOrCreateNpn(ctx context.Context) (*unstructured.Unstructured, error) {
120+
121+
m.Info("Get Or Create NPN CR NOW.")
122+
instance, err := m.GetOrCreateMachine(ctx)
123+
if err != nil {
124+
m.Info(fmt.Sprintf("Failed to get machine: %s", err))
125+
return nil, err
126+
}
127+
wlClient, err := m.NewWorkloadClient(ctx)
128+
if err != nil {
129+
return nil, err
130+
}
131+
npnCr := &unstructured.Unstructured{}
132+
slicedId := strings.Split(*instance.Id, ".")
133+
instanceSuffix := slicedId[len(slicedId)-1]
134+
npnCr.SetGroupVersionKind(schema.GroupVersionKind{
135+
Version: npnVersion,
136+
Kind: npnKind,
137+
})
138+
err = wlClient.Get(ctx, client.ObjectKey{Name: instanceSuffix}, npnCr)
139+
// Return NPN CR Object if it existed
140+
if err == nil {
141+
m.Info(fmt.Sprintf("Sucessfully Get an Existed NPN CR Object: %s", npnCr))
142+
return npnCr, nil
143+
}
144+
maxPodCount := m.OCIMachine.Spec.MaxPodPerNode
145+
podSubnetIds := m.OCIMachine.Spec.PodSubnetIds
146+
podNsgIds := m.OCIMachine.Spec.PodNSGIds
147+
npnCrCreate := &unstructured.Unstructured{}
148+
npnCrCreate.Object = map[string]interface{}{
149+
"metadata": map[string]interface{}{
150+
"name": instanceSuffix,
151+
},
152+
"spec": map[string]interface{}{
153+
"id": *instance.Id,
154+
"maxPodCount": maxPodCount,
155+
"podSubnetIds": podSubnetIds,
156+
"networkSecurityGroupIds": podNsgIds,
157+
},
158+
}
159+
160+
npnCrCreate.SetGroupVersionKind(schema.GroupVersionKind{
161+
Version: npnVersion,
162+
Kind: npnKind,
163+
})
164+
m.Info(fmt.Sprintf("NPN CR to Create is: %v", npnCrCreate))
165+
err = wlClient.Create(ctx, npnCrCreate)
166+
return npnCrCreate, err
167+
}

0 commit comments

Comments
 (0)