Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

crd: Adding AzureResource + scaffolding and code-generation helpers #14

Merged
merged 2 commits into from
Jan 2, 2020
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
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions crd/AzureResource-example.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions crd/AzureResource.yaml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions crd/README.md
Original file line number Diff line number Diff line change
@@ -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 [email protected]: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`
7 changes: 7 additions & 0 deletions crd/generate-AzureResource.sh
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/azureresource/v1/doc.go
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions pkg/apis/azureresource/v1/register.go
Original file line number Diff line number Diff line change
@@ -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: "smc.osm.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
}
40 changes: 40 additions & 0 deletions pkg/apis/azureresource/v1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 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 Azure resources.
type AzureResourceList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []AzureResource `json:"items"`
}
103 changes: 103 additions & 0 deletions pkg/apis/azureresource/v1/zz_generated.deepcopy.go

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