From b7e375ad3e1e354e2ca1dd5a5936c4b3e81e2097 Mon Sep 17 00:00:00 2001 From: Delyan Raychev Date: Mon, 30 Dec 2019 11:43:49 -0500 Subject: [PATCH 1/2] crd: Adding AzureResource + scaffolding and code-generation helpers --- GNUmakefile | 4 +++ crd/AzureResource-example.yaml | 6 ++++ crd/AzureResource.yaml | 19 +++++++++++ crd/README.md | 31 ++++++++++++++++++ crd/generate-AzureResource.sh | 7 +++++ go.mod | 1 + pkg/apis/azureresource/v1/doc.go | 5 +++ pkg/apis/azureresource/v1/register.go | 45 +++++++++++++++++++++++++++ pkg/apis/azureresource/v1/types.go | 41 ++++++++++++++++++++++++ 9 files changed, 159 insertions(+) create mode 100644 crd/AzureResource-example.yaml create mode 100644 crd/AzureResource.yaml create mode 100644 crd/README.md create mode 100755 crd/generate-AzureResource.sh create mode 100644 pkg/apis/azureresource/v1/doc.go create mode 100644 pkg/apis/azureresource/v1/register.go create mode 100644 pkg/apis/azureresource/v1/types.go diff --git a/GNUmakefile b/GNUmakefile index 82fa7adf99..376f3a5db6 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -101,3 +101,7 @@ docker-push: docker-push-eds docker-push-sds docker-push-init docker-push-bookbu sds-root-tls: @mkdir -p $(shell pwd)/bin $(shell openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/CN=httpbin.example.com/O=Exmaple Company Name LTD./C=US' -keyout bin/key.pem -out bin/cert.pem) + +.PHONY: generate-crds +generate-crds: + @./crd/generate-AzureResource.sh diff --git a/crd/AzureResource-example.yaml b/crd/AzureResource-example.yaml new file mode 100644 index 0000000000..bf53f63237 --- /dev/null +++ b/crd/AzureResource-example.yaml @@ -0,0 +1,6 @@ +apiVersion: smc.osm.k8s.io/v1 +kind: AzureResource +metadata: + name: some-virtual-machine-scale-set +spec: + resourceid: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz diff --git a/crd/AzureResource.yaml b/crd/AzureResource.yaml new file mode 100644 index 0000000000..2922193fff --- /dev/null +++ b/crd/AzureResource.yaml @@ -0,0 +1,19 @@ +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: azureresources.smc.osm.k8s.io +spec: + group: smc.osm.k8s.io + version: v1 + names: + kind: AzureResource + plural: azureresources + scope: Namespaced + validation: + openAPIV3Schema: + properties: + spec: + properties: + resourceid: + description: "Resource ID (UUID) of the Azure compute resource. Example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz" + type: string diff --git a/crd/README.md b/crd/README.md new file mode 100644 index 0000000000..a76ebed673 --- /dev/null +++ b/crd/README.md @@ -0,0 +1,31 @@ +# Generating SMC CRDs + +This document outlines the steps necessary to generate the Go code supporting the CRDs in [this](./crd/) directory. + +### Assumptions +Code generation scripts assumes: + - `$GOPATH` is correctly setup on this workstation + - this repository has been cloned in `$GOPATH/src/github.com/deislabs/smc/` + +### Prerequisites + 1. Download (clone) [the code-generation tool](https://github.com/kubernetes/code-generator): + ```bash + mkdir -p $GOPATH/src/k8s.io + pushd $GOPATH/src/k8s.io + git clone git@github.com:kubernetes/code-generator.git + popd + ``` + +### Generate Informers etc. + 1. Run the code-eneration tool: + ``` + $GOPATH/src/k8s.io/code-generator/generate-groups.sh \ + all \ + github.com/deislabs/smc/pkg/smc_client \ + github.com/deislabs/smc/pkg/apis \ + "azureresource:v1" + ``` + +### Install the Custom Resource Definitions: + 1. Install the actual CRD: `kubectl apply -f ./AzureResource.yaml` + 1. Create a sample object to test the new CRD: `kubectl apply -f ./AzureResource-example.yaml` diff --git a/crd/generate-AzureResource.sh b/crd/generate-AzureResource.sh new file mode 100755 index 0000000000..ecfb61afc8 --- /dev/null +++ b/crd/generate-AzureResource.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +$GOPATH/src/k8s.io/code-generator/generate-groups.sh \ + all \ + github.com/deislabs/smc/pkg/smc_client \ + github.com/deislabs/smc/pkg/apis \ + "azureresource:v1" diff --git a/go.mod b/go.mod index 1b3f4acb22..b0d1c0c233 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( google.golang.org/grpc v1.22.1 gopkg.in/yaml.v2 v2.2.4 // indirect k8s.io/api v0.0.0-20190614205929-e4e27c96b39a + k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad k8s.io/client-go v11.0.0+incompatible k8s.io/klog v1.0.0 // indirect ) diff --git a/pkg/apis/azureresource/v1/doc.go b/pkg/apis/azureresource/v1/doc.go new file mode 100644 index 0000000000..2ed60f98eb --- /dev/null +++ b/pkg/apis/azureresource/v1/doc.go @@ -0,0 +1,5 @@ +// +k8s:deepcopy-gen=package,register +// +groupName=smc.osm.k8s.io + +// Package v1 is the v1 version of the API. +package v1 diff --git a/pkg/apis/azureresource/v1/register.go b/pkg/apis/azureresource/v1/register.go new file mode 100644 index 0000000000..2464611fb7 --- /dev/null +++ b/pkg/apis/azureresource/v1/register.go @@ -0,0 +1,45 @@ +// +k8s:deepcopy-gen=package,register +// +groupName=smc.osm.k8s.io + +// Package v1 contains API Schema definitions for the AzureResource v1 API group +package v1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var ( + // SchemeGroupVersion is group version used to register these objects + SchemeGroupVersion = schema.GroupVersion{ + Group: "appgw.ingress.k8s.io", + Version: "v1", + } + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) + + // AddToScheme adds all Resources to the Scheme + AddToScheme = SchemeBuilder.AddToScheme +) + +// Kind takes an unqualified kind and returns back a Group qualified GroupKind +func Kind(kind string) schema.GroupKind { + return SchemeGroupVersion.WithKind(kind).GroupKind() +} + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} + +// Adds the list of known types to Scheme. +func addKnownTypes(scheme *runtime.Scheme) error { + scheme.AddKnownTypes(SchemeGroupVersion, + &AzureResource{}, + &AzureResourceList{}, + ) + metav1.AddToGroupVersion(scheme, SchemeGroupVersion) + return nil +} diff --git a/pkg/apis/azureresource/v1/types.go b/pkg/apis/azureresource/v1/types.go new file mode 100644 index 0000000000..05ad528ab2 --- /dev/null +++ b/pkg/apis/azureresource/v1/types.go @@ -0,0 +1,41 @@ +package v1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// AzureResource is an object describing Azure specific compute, which will be included in the Service Mesh. +type AzureResource struct { + metav1.TypeMeta `json:",inline"` + + // +optional + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec AzureResourceSpec `json:"spec"` +} + +// AzureResourceSpec defines the properties that uniquely identify an Azure compute resource. +type AzureResourceSpec struct { + metav1.TypeMeta `json:",inline"` + + // +optional + metav1.ObjectMeta `json:"metadata,omitempty"` + + // ResourceID is the URI uniquely identifying an unique Azure compute resource. + // example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz + ResourceID string `json:"resourceid"` + +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// AzureResourceList is the list of prohibited targets +type AzureResourceList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata"` + + Items []AzureResource `json:"items"` +} From 7a0b5d384a90fe5637c2d28a39c3e1439907a095 Mon Sep 17 00:00:00 2001 From: Delyan Raychev Date: Mon, 30 Dec 2019 14:33:04 -0500 Subject: [PATCH 2/2] crd: Adding zz_generated.deepcopy.go so Go lint/vet passes --- pkg/apis/azureresource/v1/register.go | 2 +- pkg/apis/azureresource/v1/types.go | 7 +- .../azureresource/v1/zz_generated.deepcopy.go | 103 ++++++++++++++++++ 3 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 pkg/apis/azureresource/v1/zz_generated.deepcopy.go diff --git a/pkg/apis/azureresource/v1/register.go b/pkg/apis/azureresource/v1/register.go index 2464611fb7..73d9caa61f 100644 --- a/pkg/apis/azureresource/v1/register.go +++ b/pkg/apis/azureresource/v1/register.go @@ -13,7 +13,7 @@ import ( var ( // SchemeGroupVersion is group version used to register these objects SchemeGroupVersion = schema.GroupVersion{ - Group: "appgw.ingress.k8s.io", + Group: "smc.osm.k8s.io", Version: "v1", } diff --git a/pkg/apis/azureresource/v1/types.go b/pkg/apis/azureresource/v1/types.go index 05ad528ab2..8c60956028 100644 --- a/pkg/apis/azureresource/v1/types.go +++ b/pkg/apis/azureresource/v1/types.go @@ -24,15 +24,14 @@ type AzureResourceSpec struct { // +optional metav1.ObjectMeta `json:"metadata,omitempty"` - // ResourceID is the URI uniquely identifying an unique Azure compute resource. - // example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz + // ResourceID is the URI identifying an unique Azure compute resource. + // example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz ResourceID string `json:"resourceid"` - } // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -// AzureResourceList is the list of prohibited targets +// AzureResourceList is the list of Azure resources. type AzureResourceList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata"` diff --git a/pkg/apis/azureresource/v1/zz_generated.deepcopy.go b/pkg/apis/azureresource/v1/zz_generated.deepcopy.go new file mode 100644 index 0000000000..2d2e08af88 --- /dev/null +++ b/pkg/apis/azureresource/v1/zz_generated.deepcopy.go @@ -0,0 +1,103 @@ +// +build !ignore_autogenerated + +/* +Copyright The Kubernetes Authors. + +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 + + http://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. +*/ + +// Code generated by deepcopy-gen. DO NOT EDIT. + +package v1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AzureResource) DeepCopyInto(out *AzureResource) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AzureResource. +func (in *AzureResource) DeepCopy() *AzureResource { + if in == nil { + return nil + } + out := new(AzureResource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AzureResource) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AzureResourceList) DeepCopyInto(out *AzureResourceList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]AzureResource, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AzureResourceList. +func (in *AzureResourceList) DeepCopy() *AzureResourceList { + if in == nil { + return nil + } + out := new(AzureResourceList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *AzureResourceList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *AzureResourceSpec) DeepCopyInto(out *AzureResourceSpec) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AzureResourceSpec. +func (in *AzureResourceSpec) DeepCopy() *AzureResourceSpec { + if in == nil { + return nil + } + out := new(AzureResourceSpec) + in.DeepCopyInto(out) + return out +}