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

Commit 507b62e

Browse files
authored
Merge pull request #14 from deislabs/draychev/azure-resource
crd: Adding AzureResource + scaffolding and code-generation helpers
2 parents a539e18 + 7a0b5d3 commit 507b62e

10 files changed

+261
-0
lines changed

GNUmakefile

+4
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ docker-push: docker-push-eds docker-push-sds docker-push-init docker-push-bookbu
101101
sds-root-tls:
102102
@mkdir -p $(shell pwd)/bin
103103
$(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)
104+
105+
.PHONY: generate-crds
106+
generate-crds:
107+
@./crd/generate-AzureResource.sh

crd/AzureResource-example.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: smc.osm.k8s.io/v1
2+
kind: AzureResource
3+
metadata:
4+
name: some-virtual-machine-scale-set
5+
spec:
6+
resourceid: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz

crd/AzureResource.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: azureresources.smc.osm.k8s.io
5+
spec:
6+
group: smc.osm.k8s.io
7+
version: v1
8+
names:
9+
kind: AzureResource
10+
plural: azureresources
11+
scope: Namespaced
12+
validation:
13+
openAPIV3Schema:
14+
properties:
15+
spec:
16+
properties:
17+
resourceid:
18+
description: "Resource ID (UUID) of the Azure compute resource. Example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz"
19+
type: string

crd/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generating SMC CRDs
2+
3+
This document outlines the steps necessary to generate the Go code supporting the CRDs in [this](./crd/) directory.
4+
5+
### Assumptions
6+
Code generation scripts assumes:
7+
- `$GOPATH` is correctly setup on this workstation
8+
- this repository has been cloned in `$GOPATH/src/github.com/deislabs/smc/`
9+
10+
### Prerequisites
11+
1. Download (clone) [the code-generation tool](https://github.com/kubernetes/code-generator):
12+
```bash
13+
mkdir -p $GOPATH/src/k8s.io
14+
pushd $GOPATH/src/k8s.io
15+
git clone [email protected]:kubernetes/code-generator.git
16+
popd
17+
```
18+
19+
### Generate Informers etc.
20+
1. Run the code-eneration tool:
21+
```
22+
$GOPATH/src/k8s.io/code-generator/generate-groups.sh \
23+
all \
24+
github.com/deislabs/smc/pkg/smc_client \
25+
github.com/deislabs/smc/pkg/apis \
26+
"azureresource:v1"
27+
```
28+
29+
### Install the Custom Resource Definitions:
30+
1. Install the actual CRD: `kubectl apply -f ./AzureResource.yaml`
31+
1. Create a sample object to test the new CRD: `kubectl apply -f ./AzureResource-example.yaml`

crd/generate-AzureResource.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
$GOPATH/src/k8s.io/code-generator/generate-groups.sh \
4+
all \
5+
github.com/deislabs/smc/pkg/smc_client \
6+
github.com/deislabs/smc/pkg/apis \
7+
"azureresource:v1"

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
google.golang.org/grpc v1.22.1
2525
gopkg.in/yaml.v2 v2.2.4 // indirect
2626
k8s.io/api v0.0.0-20190614205929-e4e27c96b39a
27+
k8s.io/apimachinery v0.0.0-20190612125636-6a5db36e93ad
2728
k8s.io/client-go v11.0.0+incompatible
2829
k8s.io/klog v1.0.0 // indirect
2930
)

pkg/apis/azureresource/v1/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// +k8s:deepcopy-gen=package,register
2+
// +groupName=smc.osm.k8s.io
3+
4+
// Package v1 is the v1 version of the API.
5+
package v1

pkg/apis/azureresource/v1/register.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +k8s:deepcopy-gen=package,register
2+
// +groupName=smc.osm.k8s.io
3+
4+
// Package v1 contains API Schema definitions for the AzureResource v1 API group
5+
package v1
6+
7+
import (
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
"k8s.io/apimachinery/pkg/runtime/schema"
11+
)
12+
13+
var (
14+
// SchemeGroupVersion is group version used to register these objects
15+
SchemeGroupVersion = schema.GroupVersion{
16+
Group: "smc.osm.k8s.io",
17+
Version: "v1",
18+
}
19+
20+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
21+
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
22+
23+
// AddToScheme adds all Resources to the Scheme
24+
AddToScheme = SchemeBuilder.AddToScheme
25+
)
26+
27+
// Kind takes an unqualified kind and returns back a Group qualified GroupKind
28+
func Kind(kind string) schema.GroupKind {
29+
return SchemeGroupVersion.WithKind(kind).GroupKind()
30+
}
31+
32+
// Resource takes an unqualified resource and returns a Group qualified GroupResource
33+
func Resource(resource string) schema.GroupResource {
34+
return SchemeGroupVersion.WithResource(resource).GroupResource()
35+
}
36+
37+
// Adds the list of known types to Scheme.
38+
func addKnownTypes(scheme *runtime.Scheme) error {
39+
scheme.AddKnownTypes(SchemeGroupVersion,
40+
&AzureResource{},
41+
&AzureResourceList{},
42+
)
43+
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
44+
return nil
45+
}

pkg/apis/azureresource/v1/types.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package v1
2+
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
)
6+
7+
// +genclient
8+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
9+
10+
// AzureResource is an object describing Azure specific compute, which will be included in the Service Mesh.
11+
type AzureResource struct {
12+
metav1.TypeMeta `json:",inline"`
13+
14+
// +optional
15+
metav1.ObjectMeta `json:"metadata,omitempty"`
16+
17+
Spec AzureResourceSpec `json:"spec"`
18+
}
19+
20+
// AzureResourceSpec defines the properties that uniquely identify an Azure compute resource.
21+
type AzureResourceSpec struct {
22+
metav1.TypeMeta `json:",inline"`
23+
24+
// +optional
25+
metav1.ObjectMeta `json:"metadata,omitempty"`
26+
27+
// ResourceID is the URI identifying an unique Azure compute resource.
28+
// example: /resource/subscriptions/e3f0/resourceGroups/mesh-rg/providers/Microsoft.Compute/virtualMachineScaleSets/baz
29+
ResourceID string `json:"resourceid"`
30+
}
31+
32+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
33+
34+
// AzureResourceList is the list of Azure resources.
35+
type AzureResourceList struct {
36+
metav1.TypeMeta `json:",inline"`
37+
metav1.ListMeta `json:"metadata"`
38+
39+
Items []AzureResource `json:"items"`
40+
}

pkg/apis/azureresource/v1/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)