Skip to content

Commit cc81d9a

Browse files
author
Miguel Varela Ramos
committed
Initial kubebuilder scaffolding
1 parent 5e0df83 commit cc81d9a

File tree

8 files changed

+371
-1
lines changed

8 files changed

+371
-1
lines changed

pkg/crds/PROJECT

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout:
33
- go.kubebuilder.io/v3
44
multigroup: true
55
projectName: operator
6-
repo: github.com/cortexlabs/cortex
6+
repo: github.com/cortexlabs/cortex/pkg/crds
77
resources:
88
- api:
99
crdVersion: v1
@@ -14,4 +14,13 @@ resources:
1414
kind: BatchJob
1515
path: github.com/cortexlabs/cortex/pkg/crds/apis/batch/v1alpha1
1616
version: v1alpha1
17+
- api:
18+
crdVersion: v1
19+
namespaced: true
20+
controller: true
21+
domain: cortex.dev
22+
group: api
23+
kind: RealtimeAPI
24+
path: github.com/cortexlabs/cortex/pkg/crds/apis/api/v1alpha1
25+
version: v1alpha1
1726
version: "3"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2021 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1alpha1 contains API Schema definitions for the api v1alpha1 API group
18+
//+kubebuilder:object:generate=true
19+
//+groupName=api.cortex.dev
20+
package v1alpha1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "api.cortex.dev", Version: "v1alpha1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2021 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// RealtimeAPISpec defines the desired state of RealtimeAPI
24+
type RealtimeAPISpec struct {
25+
}
26+
27+
// RealtimeAPIStatus defines the observed state of RealtimeAPI
28+
type RealtimeAPIStatus struct {
29+
}
30+
31+
//+kubebuilder:object:root=true
32+
//+kubebuilder:subresource:status
33+
34+
// RealtimeAPI is the Schema for the realtimeapis API
35+
type RealtimeAPI struct {
36+
metav1.TypeMeta `json:",inline"`
37+
metav1.ObjectMeta `json:"metadata,omitempty"`
38+
39+
Spec RealtimeAPISpec `json:"spec,omitempty"`
40+
Status RealtimeAPIStatus `json:"status,omitempty"`
41+
}
42+
43+
//+kubebuilder:object:root=true
44+
45+
// RealtimeAPIList contains a list of RealtimeAPI
46+
type RealtimeAPIList struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ListMeta `json:"metadata,omitempty"`
49+
Items []RealtimeAPI `json:"items"`
50+
}
51+
52+
func init() {
53+
SchemeBuilder.Register(&RealtimeAPI{}, &RealtimeAPIList{})
54+
}

pkg/crds/apis/api/v1alpha1/zz_generated.deepcopy.go

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

pkg/crds/config/crd/kustomization.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
# It should be run by config/default
44
resources:
55
- bases/batch.cortex.dev_batchjobs.yaml
6+
- bases/api.cortex.dev_realtimeapis.yaml
67
#+kubebuilder:scaffold:crdkustomizeresource
78

89
patchesStrategicMerge:
910
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix.
1011
# patches here are for enabling the conversion webhook for each CRD
1112
#- patches/webhook_in_batchjobs.yaml
13+
#- patches/webhook_in_realtimeapis.yaml
1214
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1315

1416
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
1517
# patches here are for enabling the CA injection for each CRD
1618
#- patches/cainjection_in_batchjobs.yaml
19+
#- patches/cainjection_in_realtimeapis.yaml
1720
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1821

1922
# the following config is for teaching kustomize how to do kustomization for CRDs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2021 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"context"
21+
22+
"github.com/go-logr/logr"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
"sigs.k8s.io/controller-runtime/pkg/client"
26+
27+
apiv1alpha1 "github.com/cortexlabs/cortex/pkg/crds/apis/api/v1alpha1"
28+
)
29+
30+
// RealtimeAPIReconciler reconciles a RealtimeAPI object
31+
type RealtimeAPIReconciler struct {
32+
client.Client
33+
Log logr.Logger
34+
Scheme *runtime.Scheme
35+
}
36+
37+
//+kubebuilder:rbac:groups=api.cortex.dev,resources=realtimeapis,verbs=get;list;watch;create;update;patch;delete
38+
//+kubebuilder:rbac:groups=api.cortex.dev,resources=realtimeapis/status,verbs=get;update;patch
39+
//+kubebuilder:rbac:groups=api.cortex.dev,resources=realtimeapis/finalizers,verbs=update
40+
41+
// Reconcile is part of the main kubernetes reconciliation loop which aims to
42+
// move the current state of the cluster closer to the desired state.
43+
// TODO(user): Modify the Reconcile function to compare the state specified by
44+
// the RealtimeAPI object against the actual cluster state, and then
45+
// perform operations to make the cluster state reflect the state specified by
46+
// the user.
47+
//
48+
// For more details, check Reconcile and its Result here:
49+
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
50+
func (r *RealtimeAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
51+
_ = r.Log.WithValues("realtimeapi", req.NamespacedName)
52+
53+
// your logic here
54+
55+
return ctrl.Result{}, nil
56+
}
57+
58+
// SetupWithManager sets up the controller with the Manager.
59+
func (r *RealtimeAPIReconciler) SetupWithManager(mgr ctrl.Manager) error {
60+
return ctrl.NewControllerManagedBy(mgr).
61+
For(&apiv1alpha1.RealtimeAPI{}).
62+
Complete(r)
63+
}
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2021 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"path/filepath"
21+
"testing"
22+
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
"k8s.io/client-go/kubernetes/scheme"
26+
"k8s.io/client-go/rest"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/envtest"
29+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
30+
logf "sigs.k8s.io/controller-runtime/pkg/log"
31+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
33+
apiv1alpha1 "github.com/cortexlabs/cortex/pkg/crds/apis/api/v1alpha1"
34+
//+kubebuilder:scaffold:imports
35+
)
36+
37+
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
38+
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
39+
40+
var cfg *rest.Config
41+
var k8sClient client.Client
42+
var testEnv *envtest.Environment
43+
44+
func TestAPIs(t *testing.T) {
45+
RegisterFailHandler(Fail)
46+
47+
RunSpecsWithDefaultAndCustomReporters(t,
48+
"Controller Suite",
49+
[]Reporter{printer.NewlineReporter{}})
50+
}
51+
52+
var _ = BeforeSuite(func() {
53+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
54+
55+
By("bootstrapping test environment")
56+
testEnv = &envtest.Environment{
57+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
58+
ErrorIfCRDPathMissing: true,
59+
}
60+
61+
cfg, err := testEnv.Start()
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(cfg).NotTo(BeNil())
64+
65+
err = apiv1alpha1.AddToScheme(scheme.Scheme)
66+
Expect(err).NotTo(HaveOccurred())
67+
68+
//+kubebuilder:scaffold:scheme
69+
70+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
71+
Expect(err).NotTo(HaveOccurred())
72+
Expect(k8sClient).NotTo(BeNil())
73+
74+
}, 60)
75+
76+
var _ = AfterSuite(func() {
77+
By("tearing down the test environment")
78+
err := testEnv.Stop()
79+
Expect(err).NotTo(HaveOccurred())
80+
})

0 commit comments

Comments
 (0)