Skip to content

Commit faa41bc

Browse files
authored
Merge pull request #351 from droot/feature/crd-conversion-webhook
✨ crd conversion webhook implementation
2 parents 2d68d6c + 7cf7a99 commit faa41bc

18 files changed

+1177
-0
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package apis
17+
18+
import (
19+
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v1"
20+
)
21+
22+
func init() {
23+
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
24+
AddToSchemes = append(AddToSchemes, v1.SchemeBuilder.AddToScheme)
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package apis
17+
18+
import (
19+
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v2"
20+
)
21+
22+
func init() {
23+
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
24+
AddToSchemes = append(AddToSchemes, v2.SchemeBuilder.AddToScheme)
25+
}

examples/conversion/pkg/apis/apis.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// Generate deepcopy for apis
17+
//go:generate go run ../../vendor/k8s.io/code-generator/cmd/deepcopy-gen/main.go -O zz_generated.deepcopy -i ./... -h ../../hack/boilerplate.go.txt
18+
19+
// Package apis contains Kubernetes API groups.
20+
package apis
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime"
24+
)
25+
26+
// AddToSchemes may be used to add all resources defined in the project to a Scheme
27+
var AddToSchemes runtime.SchemeBuilder
28+
29+
// AddToScheme adds all Resources to the Scheme
30+
func AddToScheme(s *runtime.Scheme) error {
31+
return AddToSchemes.AddToScheme(s)
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// Package jobs contains jobs API versions
17+
package jobs
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// Package v1 contains API Schema definitions for the jobs v1 API group
17+
// +k8s:openapi-gen=true
18+
// +k8s:deepcopy-gen=package,register
19+
// +k8s:conversion-gen=github.com/droot/crd-conversion-example/pkg/apis/jobs
20+
// +k8s:defaulter-gen=TypeMeta
21+
// +groupName=jobs.example.org
22+
package v1
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package v1
17+
18+
import (
19+
"fmt"
20+
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v2"
23+
"sigs.k8s.io/controller-runtime/pkg/conversion"
24+
)
25+
26+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
27+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
28+
29+
// ExternalJobSpec defines the desired state of ExternalJob
30+
type ExternalJobSpec struct {
31+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
32+
// Important: Run "make" to regenerate code after modifying this file
33+
RunAt string `json:"runAt"`
34+
}
35+
36+
// ExternalJobStatus defines the observed state of ExternalJob
37+
type ExternalJobStatus struct {
38+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
39+
// Important: Run "make" to regenerate code after modifying this file
40+
}
41+
42+
// +genclient
43+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
44+
45+
// ExternalJob is the Schema for the externaljobs API
46+
// +k8s:openapi-gen=true
47+
type ExternalJob struct {
48+
metav1.TypeMeta `json:",inline"`
49+
metav1.ObjectMeta `json:"metadata,omitempty"`
50+
51+
Spec ExternalJobSpec `json:"spec,omitempty"`
52+
Status ExternalJobStatus `json:"status,omitempty"`
53+
}
54+
55+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
56+
57+
// ExternalJobList contains a list of ExternalJob
58+
type ExternalJobList struct {
59+
metav1.TypeMeta `json:",inline"`
60+
metav1.ListMeta `json:"metadata,omitempty"`
61+
Items []ExternalJob `json:"items"`
62+
}
63+
64+
func init() {
65+
SchemeBuilder.Register(&ExternalJob{}, &ExternalJobList{})
66+
}
67+
68+
// ConvertTo implements conversion logic to convert to Hub type (v2.ExternalJob
69+
// in this case)
70+
func (ej *ExternalJob) ConvertTo(dst conversion.Hub) error {
71+
switch t := dst.(type) {
72+
case *v2.ExternalJob:
73+
jobv2 := dst.(*v2.ExternalJob)
74+
jobv2.ObjectMeta = ej.ObjectMeta
75+
jobv2.Spec.ScheduleAt = ej.Spec.RunAt
76+
return nil
77+
default:
78+
return fmt.Errorf("unsupported type %v", t)
79+
}
80+
}
81+
82+
// ConvertFrom implements conversion logic to convert from Hub type (v2.ExternalJob
83+
// in this case)
84+
func (ej *ExternalJob) ConvertFrom(src conversion.Hub) error {
85+
switch t := src.(type) {
86+
case *v2.ExternalJob:
87+
jobv2 := src.(*v2.ExternalJob)
88+
ej.ObjectMeta = jobv2.ObjectMeta
89+
ej.Spec.RunAt = jobv2.Spec.ScheduleAt
90+
return nil
91+
default:
92+
return fmt.Errorf("unsupported type %v", t)
93+
}
94+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// NOTE: Boilerplate only. Ignore this file.
17+
18+
// Package v1 contains API Schema definitions for the jobs v1 API group
19+
// +k8s:openapi-gen=true
20+
// +k8s:deepcopy-gen=package,register
21+
// +k8s:conversion-gen=github.com/droot/crd-conversion-example/pkg/apis/jobs
22+
// +k8s:defaulter-gen=TypeMeta
23+
// +groupName=jobs.example.org
24+
package v1
25+
26+
import (
27+
"k8s.io/apimachinery/pkg/runtime/schema"
28+
controllers "sigs.k8s.io/controller-runtime"
29+
)
30+
31+
var (
32+
// SchemeGroupVersion is group version used to register these objects
33+
SchemeGroupVersion = schema.GroupVersion{Group: "jobs.example.org", Version: "v1"}
34+
35+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
36+
SchemeBuilder = &controllers.SchemeBuilder{GroupVersion: SchemeGroupVersion}
37+
38+
// AddToScheme is required by pkg/client/...
39+
AddToScheme = SchemeBuilder.AddToScheme
40+
)
41+
42+
// Resource is required by pkg/client/listers/...
43+
func Resource(resource string) schema.GroupResource {
44+
return SchemeGroupVersion.WithResource(resource).GroupResource()
45+
}

examples/conversion/pkg/apis/jobs/v1/zz_generated.deepcopy.go

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)