Skip to content

Commit

Permalink
Increment API version to support new reconciliation API and move rel… (
Browse files Browse the repository at this point in the history
…#232)

* Increment API version to support new reconciliation API and move related fields
  • Loading branch information
shyamradhakrishnan committed Mar 30, 2023
1 parent ee5de6b commit f620d39
Show file tree
Hide file tree
Showing 140 changed files with 22,488 additions and 2,206 deletions.
59 changes: 49 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ GINKGO := $(BIN_DIR)/$(GINKGO_BIN)
CONTROLLER_GEN_BIN = controller-gen
CONTROLLER_GEN := $(BIN_DIR)/$(CONTROLLER_GEN_BIN)

CONVERSION_GEN_BIN := conversion-gen
CONVERSION_GEN := $(BIN_DIR)/conversion-gen

# set up `setup-envtest` to install kubebuilder dependency
export KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.24.2
SETUP_ENVTEST_VER := v0.0.0-20230131074648-f5014c077fc3
SETUP_ENVTEST_BIN := setup-envtest
SETUP_ENVTEST := $(abspath $(TOOLS_BIN_DIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER))
SETUP_ENVTEST_PKG := sigs.k8s.io/controller-runtime/tools/setup-envtest

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand All @@ -92,6 +102,11 @@ endif
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

# set --output-base used for conversion-gen which needs to be different for in GOPATH and outside GOPATH dev
ifneq ($(abspath $(ROOT_DIR)),$(GOPATH)/src/github.com/oracle/cluster-api-provider-oci)
OUTPUT_BASE := --output-base=$(ROOT_DIR)
endif

all: build

##@ General
Expand Down Expand Up @@ -119,28 +134,49 @@ manifests: $(CONTROLLER_GEN) ## Generate WebhookConfiguration, ClusterRole and C
paths="./exp/api/..." \
output:crd:artifacts:config=config/crd/bases

generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: $(CONTROLLER_GEN) $(CONVERSION_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..." paths="./exp/api/..."
$(CONVERSION_GEN) \
--input-dirs=./api/v1beta1 \
--extra-peer-dirs=sigs.k8s.io/cluster-api/api/v1beta1 \
--build-tag=ignore_autogenerated_conversions \
--output-file-base=zz_generated.conversion \
--go-header-file=hack/boilerplate.go.txt $(OUTPUT_BASE)
$(CONVERSION_GEN) \
--input-dirs=./exp/api/v1beta1 \
--extra-peer-dirs=sigs.k8s.io/cluster-api/api/v1beta1 \
--extra-peer-dirs=github.com/oracle/cluster-api-provider-oci/api/v1beta1 \
--build-tag=ignore_autogenerated_conversions \
--output-file-base=zz_generated.conversion \
--go-header-file=hack/boilerplate.go.txt $(OUTPUT_BASE)

fmt: ## Run go fmt against code.
go fmt ./...

vet: ## Run go vet against code.
go vet ./...
##@ test:

.PHONY: install-setup-envtest
install-setup-envtest: # Install setup-envtest so that setup-envtest's eval is executed after the tool has been installed.
GOBIN=$(abspath $(TOOLS_BIN_DIR)) $(GO_INSTALL) $(SETUP_ENVTEST_PKG) $(SETUP_ENVTEST_BIN) $(SETUP_ENVTEST_VER)

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
.PHONY: setup-envtest
setup-envtest: install-setup-envtest # Build setup-envtest from tools folder.
@$(eval KUBEBUILDER_ASSETS := $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))) \
if [ -z "$(KUBEBUILDER_ASSETS)" ]; then echo "Failed to find kubebuilder assets, see errors above"; exit 1; fi; \
echo "kube-builder assets: $(KUBEBUILDER_ASSETS)"

.PHONY: test
test: setup-envtest ## Run tests
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -coverprofile=coverage.out ./... $(TEST_ARGS)
go tool cover -func=coverage.out -o coverage.txt
go tool cover -html=coverage.out -o coverage.html

##@ Build

build: generate fmt vet ## Build manager binary.
build: generate fmt ## Build manager binary.
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS} -extldflags '-static'" -o bin/manager .

run: manifests generate fmt vet ## Run a controller from your host.
run: manifests generate fmt ## Run a controller from your host.
go run ./main.go --feature-gates=MachinePool=${EXP_MACHINE_POOL}

## --------------------------------------
Expand Down Expand Up @@ -332,6 +368,9 @@ kubectl: $(KUBECTL) ## Build a local copy of kubectl.
$(CONTROLLER_GEN): ## Download controller-gen locally if necessary.
GOBIN=$(BIN_DIR)/ $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) v0.10.0

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

$(KUSTOMIZE): ## Download kustomize locally if necessary.
GOBIN=$(BIN_DIR)/ $(GO_INSTALL) sigs.k8s.io/kustomize/kustomize/v4 $(KUSTOMIZE_BIN) v4.5.2

Expand Down
122 changes: 122 additions & 0 deletions api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"errors"
"github.com/oracle/cluster-api-provider-oci/api/v1beta2"
"k8s.io/apimachinery/pkg/conversion"
)

// Convert_v1beta1_VCN_To_v1beta2_VCN converts v1beta1 VCN to v1beta2 VCN
func Convert_v1beta1_VCN_To_v1beta2_VCN(in *VCN, out *v1beta2.VCN, s conversion.Scope) error {
autoConvert_v1beta1_VCN_To_v1beta2_VCN(in, out, s)
if in.InternetGatewayId != nil {
out.InternetGateway.Id = in.InternetGatewayId
}
if in.NatGatewayId != nil {
out.NATGateway.Id = in.NatGatewayId
}
if in.ServiceGatewayId != nil {
out.ServiceGateway.Id = in.ServiceGatewayId
}
if in.PrivateRouteTableId != nil {
out.RouteTable.PrivateRouteTableId = in.PrivateRouteTableId
}
if in.PublicRouteTableId != nil {
out.RouteTable.PublicRouteTableId = in.PublicRouteTableId
}
if in.NetworkSecurityGroups != nil {
nsgList, err := convertv1beta1NSGListTov1beta2NSGList(in.NetworkSecurityGroups)
if err != nil {
return err
}
out.NetworkSecurityGroup.List = nsgList
}
return nil
}

// Convert_v1beta2_VCN_To_v1beta1_VCN converts v1beta2 VCN to v1beta1 VCN
func Convert_v1beta2_VCN_To_v1beta1_VCN(in *v1beta2.VCN, out *VCN, s conversion.Scope) error {
autoConvert_v1beta2_VCN_To_v1beta1_VCN(in, out, s)
if in.InternetGateway.Id != nil {
out.InternetGatewayId = in.InternetGateway.Id
}
if in.NATGateway.Id != nil {
out.NatGatewayId = in.NATGateway.Id
}
if in.ServiceGateway.Id != nil {
out.ServiceGatewayId = in.ServiceGateway.Id
}
if in.RouteTable.PublicRouteTableId != nil {
out.PublicRouteTableId = in.RouteTable.PublicRouteTableId
}
if in.RouteTable.PrivateRouteTableId != nil {
out.PrivateRouteTableId = in.RouteTable.PrivateRouteTableId
}
if in.NetworkSecurityGroup.List != nil {
nsgList, err := convertv1beta2NSGListTov1beta1NSGList(in.NetworkSecurityGroup.List)
if err != nil {
return err
}
out.NetworkSecurityGroups = nsgList
}
return nil
}

// Convert_v1beta1_OCIClusterStatus_To_v1beta2_OCIClusterStatus converts v1beta1 OCIClusterStatus to v1beta2 OCIClusterStatus
func Convert_v1beta1_OCIClusterStatus_To_v1beta2_OCIClusterStatus(in *OCIClusterStatus, out *v1beta2.OCIClusterStatus, s conversion.Scope) error {
return autoConvert_v1beta1_OCIClusterStatus_To_v1beta2_OCIClusterStatus(in, out, s)
}

// Convert_v1beta2_OCIClusterSpec_To_v1beta1_OCIClusterSpec converts v1beta2 OCIClusterStatus to v1beta1 OCIClusterStatus
func Convert_v1beta2_OCIClusterSpec_To_v1beta1_OCIClusterSpec(in *v1beta2.OCIClusterSpec, out *OCIClusterSpec, s conversion.Scope) error {
return autoConvert_v1beta2_OCIClusterSpec_To_v1beta1_OCIClusterSpec(in, out, s)
}

// Convert_v1beta1_EgressSecurityRuleForNSG_To_v1beta2_EgressSecurityRuleForNSG converts v1beta1 EgressSecurityRuleForNSG to v1beta2 EgressSecurityRuleForNSG
func Convert_v1beta1_EgressSecurityRuleForNSG_To_v1beta2_EgressSecurityRuleForNSG(in *EgressSecurityRuleForNSG, out *v1beta2.EgressSecurityRuleForNSG, s conversion.Scope) error {
return autoConvert_v1beta1_EgressSecurityRuleForNSG_To_v1beta2_EgressSecurityRuleForNSG(in, out, s)
}

// Convert_v1beta1_IngressSecurityRuleForNSG_To_v1beta2_IngressSecurityRuleForNSG converts v1beta1 IngressSecurityRuleForNSG to v1beta2 IngressSecurityRuleForNSG
func Convert_v1beta1_IngressSecurityRuleForNSG_To_v1beta2_IngressSecurityRuleForNSG(in *IngressSecurityRuleForNSG, out *v1beta2.IngressSecurityRuleForNSG, s conversion.Scope) error {
return autoConvert_v1beta1_IngressSecurityRuleForNSG_To_v1beta2_IngressSecurityRuleForNSG(in, out, s)
}

// Convert_v1beta1_NetworkDetails_To_v1beta2_NetworkDetails converts v1beta1 NetworkDetails to v1beta2 NetworkDetails
func Convert_v1beta1_NetworkDetails_To_v1beta2_NetworkDetails(in *NetworkDetails, out *v1beta2.NetworkDetails, s conversion.Scope) error {
return autoConvert_v1beta1_NetworkDetails_To_v1beta2_NetworkDetails(in, out, s)
}

// Convert_v1beta1_OCIMachineSpec_To_v1beta2_OCIMachineSpec converts v1beta1 OCIMachineSpec to v1beta2 OCIMachineSpec
func Convert_v1beta1_OCIMachineSpec_To_v1beta2_OCIMachineSpec(in *OCIMachineSpec, out *v1beta2.OCIMachineSpec, s conversion.Scope) error {
err := autoConvert_v1beta1_OCIMachineSpec_To_v1beta2_OCIMachineSpec(in, out, s)
if err != nil {
return err
}
if in.NetworkDetails.SubnetId != nil {
return errors.New("deprecated field NetworkDetails.SubnetId is present in OCIMachineSpec")
}
if in.NetworkDetails.NSGId != nil {
return errors.New("deprecated field NetworkDetails.NSGId is present in OCIMachineSpec")
}
if in.NSGName != "" && len(in.NetworkDetails.NsgNames) == 0 {
out.NetworkDetails.NsgNames = []string{in.NSGName}
}
return nil
}
141 changes: 141 additions & 0 deletions api/v1beta1/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"testing"

fuzz "github.com/google/gofuzz"
. "github.com/onsi/gomega"
"github.com/oracle/cluster-api-provider-oci/api/v1beta2"
"k8s.io/apimachinery/pkg/runtime"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
OCIMachineFuzzer,
OCIMachineTemplateFuzzer,
OCIClusterFuzzer,
OCIClusterTemplateFuzzer,
}
}

func OCIMachineFuzzer(obj *OCIMachine, c fuzz.Continue) {
c.FuzzNoCustom(obj)
// nil fields which have been removed so that tests dont fail
obj.Spec.NetworkDetails.NSGId = nil
obj.Spec.NetworkDetails.SubnetId = nil
obj.Spec.NSGName = ""
}

func OCIClusterFuzzer(obj *OCICluster, c fuzz.Continue) {
c.FuzzNoCustom(obj)
// nil fields which have been removed so that tests dont fail
for _, nsg := range obj.Spec.NetworkSpec.Vcn.NetworkSecurityGroups {
if nsg != nil {
ingressRules := make([]IngressSecurityRuleForNSG, len(nsg.IngressRules))
for _, rule := range nsg.IngressRules {
rule.ID = nil
ingressRules = append(ingressRules, rule)
}
nsg.IngressRules = ingressRules

egressRules := make([]EgressSecurityRuleForNSG, len(nsg.EgressRules))
for _, rule := range nsg.EgressRules {
(&rule).ID = nil
egressRules = append(egressRules, rule)
}
nsg.EgressRules = egressRules
}
}
}

func OCIClusterTemplateFuzzer(obj *OCIClusterTemplate, c fuzz.Continue) {
c.FuzzNoCustom(obj)
// nil fields which have been removed so that tests dont fail
for _, nsg := range obj.Spec.Template.Spec.NetworkSpec.Vcn.NetworkSecurityGroups {
if nsg != nil {
ingressRules := make([]IngressSecurityRuleForNSG, len(nsg.IngressRules))
for _, rule := range nsg.IngressRules {
rule.ID = nil
ingressRules = append(ingressRules, rule)
}
nsg.IngressRules = ingressRules

egressRules := make([]EgressSecurityRuleForNSG, len(nsg.EgressRules))
for _, rule := range nsg.EgressRules {
(&rule).ID = nil
egressRules = append(egressRules, rule)
}
nsg.EgressRules = egressRules
}
}
}

func OCIMachineTemplateFuzzer(obj *OCIMachineTemplate, c fuzz.Continue) {
c.FuzzNoCustom(obj)
// nil fields which ave been removed so that tests dont fail
obj.Spec.Template.Spec.NetworkDetails.NSGId = nil
obj.Spec.Template.Spec.NetworkDetails.SubnetId = nil
obj.Spec.Template.Spec.NSGName = ""
}

func TestFuzzyConversion(t *testing.T) {
g := NewWithT(t)
scheme := runtime.NewScheme()
g.Expect(AddToScheme(scheme)).To(Succeed())
g.Expect(v1beta2.AddToScheme(scheme)).To(Succeed())

t.Run("for OCICluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Scheme: scheme,
Hub: &v1beta2.OCICluster{},
Spoke: &OCICluster{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))

t.Run("for OCIClusterTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Scheme: scheme,
Hub: &v1beta2.OCIClusterTemplate{},
Spoke: &OCIClusterTemplate{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))

t.Run("for OCIMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Scheme: scheme,
Hub: &v1beta2.OCIMachine{},
Spoke: &OCIMachine{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))

t.Run("for OCIMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Scheme: scheme,
Hub: &v1beta2.OCIMachineTemplate{},
Spoke: &OCIMachineTemplate{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))

t.Run("for OCIClusterIdentity", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Scheme: scheme,
Hub: &v1beta2.OCIClusterIdentity{},
Spoke: &OCIClusterIdentity{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))

}
19 changes: 19 additions & 0 deletions api/v1beta1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// +k8s:conversion-gen=github.com/oracle/cluster-api-provider-oci/api/v1beta2

package v1beta1
2 changes: 2 additions & 0 deletions api/v1beta1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ var (

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme

localSchemeBuilder = SchemeBuilder.SchemeBuilder
)
Loading

0 comments on commit f620d39

Please sign in to comment.