Skip to content

Commit 2d8e131

Browse files
[CDAP-19300] Added Containers Injection, added Dockerfile.test and updated the README
1 parent de3fe9c commit 2d8e131

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+22870
-1460
lines changed

.github/workflows/go.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ jobs:
2121

2222
- name: Install Kubebuilder
2323
run: |
24+
version=2.3.1
2425
os=$(go env GOOS)
2526
arch=$(go env GOARCH)
26-
curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | sudo tar -xz -C /tmp/
27-
sudo mv /tmp/kubebuilder_2.3.1_${os}_${arch} /usr/local/kubebuilder
27+
curl -L -O "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/kubebuilder_${version}_${os}_${arch}.tar.gz"
28+
tar -zxvf kubebuilder_${version}_${os}_${arch}.tar.gz
29+
sudo mv kubebuilder_${version}_${os}_${arch} /usr/local/kubebuilder
30+
rm -f kubebuilder_${version}_${os}_${arch}.tar.gz
2831
sudo find /usr/local/kubebuilder/bin -type f -exec ln -s {} /usr/local/bin \;
2932
- name: Test
3033
run: make manifests test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ anaconda-mode/
6363
*.dylib
6464
# Test binary, build with 'go test -c'
6565
*.test
66+
!Dockerfile.test
6667
# Output of the go coverage tool, specifically when used with LiteIDE
6768
*.out
6869
### Vim ###

Dockerfile.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Build the manager binary
2+
FROM golang:1.16 as tester
3+
4+
ENV version 1.0.8
5+
ENV arch amd64
6+
7+
# Copy everything in the go src
8+
WORKDIR /go/src/cdap.io/cdap-operator
9+
COPY ./ ./
10+
11+
# Install Kubebuilder
12+
RUN curl -L -O "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/kubebuilder_${version}_linux_${arch}.tar.gz" && \
13+
tar -zxvf kubebuilder_${version}_linux_${arch}.tar.gz && \
14+
mv kubebuilder_${version}_linux_${arch} /usr/local/kubebuilder && \
15+
cp /usr/local/kubebuilder/bin/* /usr/local/bin
16+
17+
# Install setup-envtest
18+
RUN go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
19+
20+
# download envtest 1.19.x for kubebuilder and to set KUBEBUILDER_ASSETS environment variable
21+
RUN $(go env GOPATH)/bin/setup-envtest use -p env 1.19.x > /tmp/setup_envtest.sh && \
22+
eval `$(go env GOPATH)/bin/setup-envtest use -p env 1.19.x` && \
23+
rm /tmp/setup_envtest.sh
24+
25+
CMD make test

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ You can checkout the CDAP Operator source code, build and run locally. To build
3333
```
3434
1. Deploy CDAP CRD to the cluster
3535
```
36-
kubectl apply -f config/crds
36+
kubectl apply -k config/crd
3737
```
3838
1. Edit the sample CDAP CR and deploy to the cluster
3939
```
@@ -61,3 +61,35 @@ You can also build a docker image containing the CDAP controller and deploy it t
6161
### Using CDAP operator to manage CDAP instances in Kubernetes
6262

6363
A step by step guide of running CDAP in Kubernetes using CDAP operator can be found in the [blog post](https://link.medium.com/hpPbiUYT9X).
64+
65+
### Running Unit Tests
66+
67+
1. Install [kubebuilder](https://book-v1.book.kubebuilder.io/quick_start.html).
68+
69+
2. Install [setup-envtest](https://github.com/kubernetes-sigs/controller-runtime/tree/master/tools/setup-envtest#envtest-binaries-manager) by running:
70+
```
71+
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
72+
```
73+
74+
3. After installing `setup-envtest`, use it to download envtest 1.19.x for kubebuilder and to set your KUBEBUILDER_ASSETS environment variable:
75+
```bash
76+
# Downloads envtest v1.19.x and writes the export statement to a temporary file
77+
$(go env GOPATH)/bin/setup-envtest use -p env 1.19.x > /tmp/setup_envtest.sh
78+
# Sets the KUBEBUILDER_ASSETS environment variable
79+
source /tmp/setup_envtest.sh
80+
# Deletes the temporary file
81+
rm /tmp/setup_envtest.sh
82+
```
83+
84+
4. Run `make test`
85+
86+
#### Running Unit Tests in a docker image
87+
88+
From the project root folder build the test image by running the following
89+
```
90+
docker build -f Dockerfile.test . -t test
91+
```
92+
Execute the image with
93+
```
94+
docker run test
95+
```

api/v1alpha1/cdapmaster_types.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type CDAPMasterSpec struct {
4242
SecuritySecret string `json:"securitySecret,omitempty"`
4343
// ServiceAccountName is the service account for all the service pods.
4444
ServiceAccountName string `json:"serviceAccountName,omitempty"`
45+
// Env is a list of environment variables for the all service containers.
46+
Env []corev1.EnvVar `json:"env,omitempty"`
4547
// LocationURI is an URI specifying an object storage for CDAP.
4648
LocationURI string `json:"locationURI"`
4749
// Config is a set of configurations that goes into cdap-site.xml.
@@ -76,6 +78,21 @@ type CDAPMasterSpec struct {
7678
Router RouterSpec `json:"router,omitempty"`
7779
// UserInterface is specification for the CDAP UI service.
7880
UserInterface UserInterfaceSpec `json:"userInterface,omitempty"`
81+
// SupportBundle is specification for the CDAP support-bundle service.
82+
// This is an optional service and may not be required for CDAP to be operational.
83+
// To disable this service: either omit or set the field to nil
84+
// To enable this service: set it to a pointer to a SupportBundleSpec struct (can be an empty struct)
85+
SupportBundle *SupportBundleSpec `json:"supportBundle,omitempty"`
86+
// TetheringAgent is specification for the CDAP Tethering Agent service.
87+
// This is an optional service and may not be required for CDAP to be operational.
88+
// To disable this service: either omit or set the field to nil
89+
// To enable this service: set it to a pointer to a TetheringAgentSpec struct (can be an empty struct)
90+
TetheringAgent *TetheringAgentSpec `json:"tetheringAgent,omitempty"`
91+
// ArtifactCache is specification for the CDAP Artifact Cache service.
92+
// This is an optional service and may not be required for CDAP to be operational.
93+
// To disable this service: either omit or set the field to nil
94+
// To enable this service: set it to a pointer to a ArtifactCacheSpec struct (can be an empty struct)
95+
ArtifactCache *ArtifactCacheSpec `json:"artifactCache,omitempty"`
7996
// Runtime is specification for the CDAP runtime service.
8097
// This is an optional service and may not be required for CDAP to be operational.
8198
// To disable this service: either omit or set the field to nil
@@ -86,6 +103,22 @@ type CDAPMasterSpec struct {
86103
// To disable this service: either omit or set the field to nil
87104
// To enable this service: set it to a pointer to a AuthenticationSpec struct (can be an empty struct)
88105
Authentication *AuthenticationSpec `json:"authentication,omitempty"`
106+
// SystemMetricsExporter is specification for the CDAP SystemMetricsExporter service.
107+
// This is an optional service and may not be required for CDAP to be operational.
108+
// To disable this service: either omit or set the field to nil
109+
// To enable this service: set it to a pointer to a SystemMetricsExporterSpec struct (can be an empty struct).
110+
// CDAPServiceSpec.EnableSystemMetrics field also needs to be set to true for stateful services which require
111+
// collection of system metrics. Services which have CDAPServiceSpec.EnableSystemMetrics as nil, missing or set to false,
112+
// will have metrics sidecar container disabled.
113+
SystemMetricsExporter *SystemMetricExporterSpec `json:"systemMetricsExporter,omitempty"`
114+
// SecurityContext defines the security context for all pods for all services.
115+
SecurityContext *SecurityContext `json:"securityContext,omitempty"`
116+
// AdditionalVolumes defines a list of additional volumes for all services.
117+
// For information on supported volume types, see https://kubernetes.io/docs/concepts/storage/volumes/.
118+
AdditionalVolumes []corev1.Volume `json:"additionalVolumes,omitempty"`
119+
// AdditionalVolumeMounts defines a list of additional volume mounts for all services.
120+
// For information on suported volume mount types, see https://kubernetes.io/docs/concepts/storage/volumes/.
121+
AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"`
89122
}
90123

91124
// CDAPServiceSpec defines the base set of specifications applicable to all master services.
@@ -115,13 +148,34 @@ type CDAPServiceSpec struct {
115148
// Key is the secret object name. Value is the mount path.
116149
// This adds Secret data to the directory specified by the volume mount path.
117150
SecretVolumes map[string]string `json:"secretVolumes,omitempty"`
151+
// AdditionalVolumes defines a list of additional volumes to mount to the service.
152+
// For information on supported volume types, see https://kubernetes.io/docs/concepts/storage/volumes/.
153+
AdditionalVolumes []corev1.Volume `json:"additionalVolumes,omitempty"`
154+
// AdditionalVolumeMounts defines a list of additional volume mounts for the service.
155+
// For information on suported volume mount types, see https://kubernetes.io/docs/concepts/storage/volumes/.
156+
AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"`
157+
// SecurityContext overrides the security context for the service pods.
158+
SecurityContext *SecurityContext `json:"securityContext,omitempty"`
159+
// EnableSystemMetrics is an optional field that is considered along with CDAPMasterSpec.SystemMetricsExporter
160+
// to start a metrics collection container in statefulsets. SystemMetricsExporter is a global setting in CDAPMasterSpec.
161+
// When SystemMetricsExporter is absent, it disables metrics collection for all stateful services.
162+
// When SystemMetricsExporter is present, this value should also be set to true for services which require system metrics
163+
// collection.
164+
EnableSystemMetrics *bool `json:"enableSystemMetrics,omitempty"`
165+
// Lifecycle is to specify Container Lifecycle hooks provided by Kubernetes for containers.
166+
// This will not be applied to the init containers as init containers do not support lifecycle.
167+
Lifecycle *corev1.Lifecycle `json:"lifecycle,omitempty"`
118168
}
119169

120170
// CDAPScalableServiceSpec defines the base specification for master services that can have more than one instance.
121171
type CDAPScalableServiceSpec struct {
122172
CDAPServiceSpec `json:",inline"`
123173
// Replicas is number of replicas for the service.
124174
Replicas *int32 `json:"replicas,omitempty"`
175+
// Containers define any additional containers a service has
176+
// This is a list of containers and can be left blank
177+
// A typical use is to add sidecars for a deployment
178+
Containers []*corev1.Container `json:"containers,omitempty"`
125179
}
126180

127181
// CDAPExternalServiceSpec defines the base specification for master services that expose to outside of the cluster.
@@ -144,6 +198,10 @@ type CDAPStatefulServiceSpec struct {
144198
StorageSize string `json:"storageSize,omitempty"`
145199
// StorageClassName is the name of the StorageClass for the persistent volume used by the service.
146200
StorageClassName *string `json:"storageClassName,omitempty"`
201+
// Containers define any additional containers a service has
202+
// This is a list of containers and can be left blank
203+
// A typical use is to add sidecars for a stateful set
204+
Containers []*corev1.Container `json:"containers,omitempty"`
147205
}
148206

149207
// AppFabricSpec defines the specification for the AppFabric service.
@@ -196,6 +254,26 @@ type UserInterfaceSpec struct {
196254
CDAPExternalServiceSpec `json:",inline"`
197255
}
198256

257+
// SupportBundleSpec defines the specification for the SupportBundle service.
258+
type SupportBundleSpec struct {
259+
CDAPStatefulServiceSpec `json:",inline"`
260+
}
261+
262+
// TetheringAgentSpec defines the specification for the TetheringAgent service.
263+
type TetheringAgentSpec struct {
264+
CDAPStatefulServiceSpec `json:",inline"`
265+
}
266+
267+
// ArtifactCacheSpec defines the specification for the ArtifactCache service.
268+
type ArtifactCacheSpec struct {
269+
CDAPStatefulServiceSpec `json:",inline"`
270+
}
271+
272+
// SystemMetricExporterSpec defines the specification for the SystemMetricsExporter service.
273+
type SystemMetricExporterSpec struct {
274+
CDAPServiceSpec `json:",inline"`
275+
}
276+
199277
// CDAPMasterStatus defines the observed state of CDAPMaster
200278
type CDAPMasterStatus struct {
201279
status.Meta `json:",inline"`
@@ -230,6 +308,32 @@ type CDAPMasterList struct {
230308
Items []CDAPMaster `json:"items"`
231309
}
232310

311+
// SecurityContext defines fields for setting corev1.SecurityContext for containers and
312+
// corev1.PodSecurityContext for pods.
313+
// For additional information, see https://kubernetes.io/docs/tasks/configure-pod-container/security-context/.
314+
type SecurityContext struct {
315+
// RunAsUser runs the pod as the specified user ID. It is applied at the pod level.
316+
RunAsUser *int64 `json:"runAsUser,omitempty"`
317+
// RunAsGroup runs the pod as the specified group ID. It is applied at the pod level.
318+
RunAsGroup *int64 `json:"runAsGroup,omitempty"`
319+
// FSGroup mounts volumes as the specified group ID and gives the primary user access
320+
// to that group. It is applied at the pod level.
321+
FSGroup *int64 `json:"fsGroup,omitempty"`
322+
// AllowPrivilegeEscalation prevents the container process from running SUID binaries.
323+
// It is applied at the container level.
324+
AllowPrivilegeEscalation *bool `json:"allowPrivilegeEscalation,omitempty"`
325+
// RunAsNonRoot indicates that the container must run as a non-root user.
326+
// If true, the Kubelet will validate the image at runtime to ensure that it
327+
// does not run as UID 0 (root) and fail to start the container if it does.
328+
RunAsNonRoot *bool `json:"runAsNonRoot,omitempty"`
329+
// Privileged runs container in privileged mode. It is applied at the container level.
330+
// Processes in privileged containers are essentially equivalent to root on the host.
331+
Privileged *bool `json:"privileged,omitempty"`
332+
// ReadOnlyRootFilesystem specifies whether the container's root filesystem is read-only.
333+
// It is applied at the container level.
334+
ReadOnlyRootFilesystem *bool `json:"readOnlyRootFilesystem,omitempty"`
335+
}
336+
233337
func init() {
234338
SchemeBuilder.Register(&CDAPMaster{}, &CDAPMasterList{})
235339
}

0 commit comments

Comments
 (0)