Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ crd conversion webhook implementation #351

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions examples/conversion/pkg/apis/addtoscheme_jobs_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apis

import (
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v1"
)

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1.SchemeBuilder.AddToScheme)
}
25 changes: 25 additions & 0 deletions examples/conversion/pkg/apis/addtoscheme_jobs_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apis

import (
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v2"
)

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v2.SchemeBuilder.AddToScheme)
}
32 changes: 32 additions & 0 deletions examples/conversion/pkg/apis/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Generate deepcopy for apis
//go:generate go run ../../vendor/k8s.io/code-generator/cmd/deepcopy-gen/main.go -O zz_generated.deepcopy -i ./... -h ../../hack/boilerplate.go.txt

// Package apis contains Kubernetes API groups.
package apis

import (
"k8s.io/apimachinery/pkg/runtime"
)

// AddToSchemes may be used to add all resources defined in the project to a Scheme
var AddToSchemes runtime.SchemeBuilder

// AddToScheme adds all Resources to the Scheme
func AddToScheme(s *runtime.Scheme) error {
return AddToSchemes.AddToScheme(s)
}
17 changes: 17 additions & 0 deletions examples/conversion/pkg/apis/jobs/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package jobs contains jobs API versions
package jobs
22 changes: 22 additions & 0 deletions examples/conversion/pkg/apis/jobs/v1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1 contains API Schema definitions for the jobs v1 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/droot/crd-conversion-example/pkg/apis/jobs
// +k8s:defaulter-gen=TypeMeta
// +groupName=jobs.example.org
package v1
94 changes: 94 additions & 0 deletions examples/conversion/pkg/apis/jobs/v1/externaljob_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/examples/conversion/pkg/apis/jobs/v2"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ExternalJobSpec defines the desired state of ExternalJob
type ExternalJobSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
RunAt string `json:"runAt"`
}

// ExternalJobStatus defines the observed state of ExternalJob
type ExternalJobStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ExternalJob is the Schema for the externaljobs API
// +k8s:openapi-gen=true
type ExternalJob struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ExternalJobSpec `json:"spec,omitempty"`
Status ExternalJobStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// ExternalJobList contains a list of ExternalJob
type ExternalJobList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ExternalJob `json:"items"`
}

func init() {
SchemeBuilder.Register(&ExternalJob{}, &ExternalJobList{})
}

// ConvertTo implements conversion logic to convert to Hub type (v2.ExternalJob
// in this case)
func (ej *ExternalJob) ConvertTo(dst conversion.Hub) error {
switch t := dst.(type) {
case *v2.ExternalJob:
jobv2 := dst.(*v2.ExternalJob)
jobv2.ObjectMeta = ej.ObjectMeta
jobv2.Spec.ScheduleAt = ej.Spec.RunAt
return nil
default:
return fmt.Errorf("unsupported type %v", t)
}
}

// ConvertFrom implements conversion logic to convert from Hub type (v2.ExternalJob
// in this case)
func (ej *ExternalJob) ConvertFrom(src conversion.Hub) error {
switch t := src.(type) {
case *v2.ExternalJob:
jobv2 := src.(*v2.ExternalJob)
ej.ObjectMeta = jobv2.ObjectMeta
ej.Spec.RunAt = jobv2.Spec.ScheduleAt
return nil
default:
return fmt.Errorf("unsupported type %v", t)
}
}
45 changes: 45 additions & 0 deletions examples/conversion/pkg/apis/jobs/v1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// NOTE: Boilerplate only. Ignore this file.

// Package v1 contains API Schema definitions for the jobs v1 API group
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen=package,register
// +k8s:conversion-gen=github.com/droot/crd-conversion-example/pkg/apis/jobs
// +k8s:defaulter-gen=TypeMeta
// +groupName=jobs.example.org
package v1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
controllers "sigs.k8s.io/controller-runtime"
)

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: "jobs.example.org", Version: "v1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &controllers.SchemeBuilder{GroupVersion: SchemeGroupVersion}

// AddToScheme is required by pkg/client/...
AddToScheme = SchemeBuilder.AddToScheme
)

// Resource is required by pkg/client/listers/...
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
116 changes: 116 additions & 0 deletions examples/conversion/pkg/apis/jobs/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading