You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crs/README.md
+18-18Lines changed: 18 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# Custom Resources
2
2
3
3
## Purpose
4
-
This document aims to provide basic understanding of the workflow of writing a new custom resource for HAProxy Ingress Controller.
5
-
Custom resources should be derived, whenever possible, from the [HAProxy CN (Client Native)](https://github.com/haproxytech/client-native) by reusing [HAProxy Models](https://github.com/haproxytech/client-native#haproxy-models).
6
-
This guide describes how to create a CR (Custom Resource) to configure the global HAProxy section.
4
+
This document aims to provide basic understanding of the workflow of writing a new custom resource for HAProxy Ingress Controller.
5
+
Custom resources should be derived, whenever possible, from the [HAProxy CN (Client Native)](https://github.com/haproxytech/client-native) by reusing [HAProxy Models](https://github.com/haproxytech/client-native#haproxy-models).
6
+
This guide describes how to create a CR (Custom Resource) to configure the global HAProxy section.
7
7
8
8
## Prerequisites
9
-
We suppose that an HAProxy CN Model, corresponding to the kubernetes custom resource, is already available. This is the case for the HAProxy global section, that we are dealing with in this guide, where we are going to reuse the following [global model](https://github.com/haproxytech/client-native/blob/master/models/global.go).
9
+
We suppose that an HAProxy CN Model, corresponding to the kubernetes custom resource, is already available. This is the case for the HAProxy global section, that we are dealing with in this guide, where we are going to reuse the following [global model](https://github.com/haproxytech/client-native/blob/master/models/global.go).
10
10
If it is not the case, this needs to be done in [HAProxy Native client](https://github.com/haproxytech/client-native) by providing [swagger specification](https://github.com/haproxytech/client-native/blob/master/specification/build/haproxy_spec.yaml) of the Model and generate it via [make models](https://github.com/haproxytech/client-native/blob/master/Makefile).
11
11
12
12
## Directory Layout
@@ -35,9 +35,9 @@ crs
35
35
36
36
## Custom Resource Creation
37
37
### CRD
38
-
Let's start by creating the Custom Resource Definition describing the Global CR and defining the kind `Global` in the API Group `core.haproxy.org`
39
-
To follow the above directory layout convention, the Global CRD should be in *crs/definitions/core/global.yaml*
40
-
The following is the beginning of the CRD, full content can be found in the corresponding yaml file:
38
+
Let's start by creating the Custom Resource Definition describing the Global CR and defining the kind `Global` in the API Group `core.haproxy.org`
39
+
To follow the above directory layout convention, the Global CRD should be in *crs/definitions/core/global.yaml*
40
+
The following is the beginning of the CRD, full content can be found in the corresponding yaml file:
41
41
```
42
42
apiVersion: apiextensions.k8s.io/v1
43
43
kind: CustomResourceDefinition
@@ -59,10 +59,10 @@ spec:
59
59
properties:
60
60
spec:
61
61
type: object
62
-
required:
62
+
required:
63
63
- config
64
-
properties:
65
-
config:
64
+
properties:
65
+
config:
66
66
title: Global
67
67
description: HAProxy global configuration
68
68
type: object
@@ -82,23 +82,23 @@ spec:
82
82
type: string
83
83
enum: [enabled, disabled]
84
84
```
85
-
The CRD is created via `apiextensions.k8s.io/v1` where the definition of a [structural schema](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) is mandatory and used for OpenAPI v3.0 validation.
85
+
The CRD is created via `apiextensions.k8s.io/v1` where the definition of a [structural schema](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) is mandatory and used for OpenAPI v3.0 validation.
86
86
The Custom Resource Definition is constructed by providing:
87
87
- API Group Name: `core.haproxy.org`, this is the group where you can find custom resources related to HAProxy configuration.
88
88
- Kind Name of the Resource: `Global` (and `globals` for plural name)
89
89
- Resource Scope: in this case it is `Namespaced`
90
90
- Group Version: `v1alpha1`
91
-
- openAPIV3Schema: For schema validation you just copy the [global schema](https://github.com/haproxytech/client-native/blob/master/specification/models/configuration.yaml#L2) from Client Native repository and insert it in the `config` field of the CRD.
91
+
- openAPIV3Schema: For schema validation you just copy the [global schema](https://github.com/haproxytech/client-native/blob/master/specification/models/configuration.yaml#L2) from Client Native repository and insert it in the `config` field of the CRD.
92
92
**NB:** a helper script is available under `crs/get-crd-schema.sh` in order to automatically retreive the schema from the CN repository. The script takes the name of Client Native model as parameter (ex: `get-crd-schema.sh global`) and returns to stdout the schema after applying the necessary changes to make it a [valid schema](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation) for kubernetes.
93
93
94
94
### GoLang Type
95
-
In this guide the API group is `core.haproxy.org` (this group is used for any resource dealing with haproxy configuration) and the API version is `alpha1v1`.
95
+
In this guide the API group is `core.haproxy.org` (this group is used for any resource dealing with haproxy configuration) and the API version is `alpha1v1`.
96
96
So to follow the */crs/api/<group>/<version>/* convention, the GoLang Type describing the `Global` CR should be in *crs/api/core/alpha1v1/global.go* with the following content:
- The GoLang types (Global for a specific resource and GlobalList for a list of resources) need to embed `TypeMeta` and `ObjectMeta` as they are mandatory for the Kubernetes type system and any resource served by Kubernetes API.
141
141
- The Spec reuses the [global configuration](https://github.com/haproxytech/client-native/blob/master/models/global.go) from HAProxy CN Models via the CR `config` field.
142
142
- Additional fields can added later to Spec if need be, but any input related to the HAProxy Global section config is provided via the `config` field.
143
-
- The deepcopy-gen tags are used to automatically generate deepCopy methods, required for any Kubernetes object, via the [code-generator](https://github.com/kubernetes/code-generator) tool. However Code Generators cannot generate the deepCopy method for types in external packages (in this case "github.com/haproxytech/client-native/v2/models") so this needs to be done manually by relying on the `MarshalBinary` and `UnmarshalBinary` of HAProxy Models.
143
+
- The deepcopy-gen tags are used to automatically generate deepCopy methods, required for any Kubernetes object, via the [code-generator](https://github.com/kubernetes/code-generator) tool. However Code Generators cannot generate the deepCopy method for types in external packages (in this case "github.com/haproxytech/client-native/v3/models") so this needs to be done manually by relying on the `MarshalBinary` and `UnmarshalBinary` of HAProxy Models.
144
144
145
145
### Code Generation
146
-
Custom Resources require more code and methods in order to be served by Kubernetes API but fortunately most of it can be generated automatically by Kubernetes code generators.
146
+
Custom Resources require more code and methods in order to be served by Kubernetes API but fortunately most of it can be generated automatically by Kubernetes code generators.
147
147
The tool we are using is [k8s.io/code-generator](https://github.com/kubernetes/code-generator) which comes with the following code generators for CRs:
148
148
- deepcopy-gen: generates the new Type's DeepCopy methods.
149
149
- register-gen: generates methods to register the new Type in [Kubernetes scheme](https://github.com/kubernetes/apimachinery/blob/ef51ab160544f9d05b68e132a4af0b0fab459954/pkg/runtime/scheme.go#L47) and make it known to the Kubernetes type system.
150
150
- client-gen: generates the client set that will give us access to the new CR
151
151
- informer-gen: generates informer methods in order to watch and react to the CR changes.
152
152
- lister-gen: generates lister methods which provide a read-only caching layer for GET and LIST requests.
153
153
154
-
Before generating code, some global tags need to be set in the package of the versioned API group which should be in *crs/api/<group>/<version>/* directory.
154
+
Before generating code, some global tags need to be set in the package of the versioned API group which should be in *crs/api/<group>/<version>/* directory.
155
155
Usually this goes into the package's doc.go, so in this case it would be in *crs/api/core/alpha1v1/doc.go* with the following content:
156
156
```
157
157
// Package v1alpha1 contains the core v1alpha1 API group
@@ -166,7 +166,7 @@ Now you can generate all necessary code for all CRs under the */crs/api* directo
166
166
167
167
168
168
## Custom Resource Management
169
-
A custom resource manager already [exists](../controller/crmanager.go) as a single entry point for any custom resource defined inside HAProxy Ingress Controller.
169
+
A custom resource manager already [exists](../controller/crmanager.go) as a single entry point for any custom resource defined inside HAProxy Ingress Controller.
170
170
Its role is to be the unique delegate for all CR related task via the following CR interface:
0 commit comments