Skip to content

Commit

Permalink
Add conversion methods between version v1 and v2
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed May 31, 2024
1 parent 39bf8b2 commit fd54099
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 16 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ clientset_%:
--input-dirs "$(GO_PKG)/$(REPO)/api/$*" \
--output-file-base zz_generated.deepcopy

.PHONY: gen-conversion
gen-conversion:
@conversion-gen --go-header-file ./hack/license/go.txt \
--input-dirs ./api/v1 \
-O zz_generated.conversion --output-base "$HOME/go/src/"

# Generate openapi schema
.PHONY: openapi
openapi: openapi_v1 openapi_v2
Expand Down
224 changes: 224 additions & 0 deletions api/v1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
Copyright AppsCode Inc. and Contributors
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 (
"unsafe"

v2 "kmodules.xyz/offshoot-api/api/v2"

core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
)

func init() {
localSchemeBuilder.Register(RegisterConversions)
}

// RegisterConversions adds conversion functions to the given scheme.
// Public to allow building arbitrary schemes.
func RegisterConversions(s *runtime.Scheme) error {
if err := s.AddGeneratedConversionFunc((*ObjectMeta)(nil), (*metav1.ObjectMeta)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1_ObjectMeta_To_metav1_ObjectMeta(a.(*ObjectMeta), b.(*metav1.ObjectMeta), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*metav1.ObjectMeta)(nil), (*ObjectMeta)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_metav1_ObjectMeta_To_v1_ObjectMeta(a.(*metav1.ObjectMeta), b.(*ObjectMeta), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*PodSpec)(nil), (*v2.PodSpec)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1_PodSpec_To_v2_PodSpec(a.(*PodSpec), b.(*v2.PodSpec), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*v2.PodSpec)(nil), (*PodSpec)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v2_PodSpec_To_v1_PodSpec(a.(*v2.PodSpec), b.(*PodSpec), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*PodTemplateSpec)(nil), (*v2.PodTemplateSpec)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v1_PodTemplateSpec_To_v2_PodTemplateSpec(a.(*PodTemplateSpec), b.(*v2.PodTemplateSpec), scope)
}); err != nil {
return err
}
if err := s.AddGeneratedConversionFunc((*v2.PodTemplateSpec)(nil), (*PodTemplateSpec)(nil), func(a, b interface{}, scope conversion.Scope) error {
return Convert_v2_PodTemplateSpec_To_v1_PodTemplateSpec(a.(*v2.PodTemplateSpec), b.(*PodTemplateSpec), scope)
}); err != nil {
return err
}
return nil
}

func autoConvert_v1_ObjectMeta_To_metav1_ObjectMeta(in *ObjectMeta, out *metav1.ObjectMeta, s conversion.Scope) error {
out.Labels = *(*map[string]string)(unsafe.Pointer(&in.Labels))
out.Annotations = *(*map[string]string)(unsafe.Pointer(&in.Annotations))
return nil
}

// Convert_v1_ObjectMeta_To_v1_ObjectMeta is an autogenerated conversion function.
func Convert_v1_ObjectMeta_To_metav1_ObjectMeta(in *ObjectMeta, out *metav1.ObjectMeta, s conversion.Scope) error {
return autoConvert_v1_ObjectMeta_To_metav1_ObjectMeta(in, out, s)
}

func autoConvert_metav1_ObjectMeta_To_v1_ObjectMeta(in *metav1.ObjectMeta, out *ObjectMeta, s conversion.Scope) error {
// WARNING: in.Name requires manual conversion: does not exist in peer-type
// WARNING: in.GenerateName requires manual conversion: does not exist in peer-type
// WARNING: in.Namespace requires manual conversion: does not exist in peer-type
// WARNING: in.SelfLink requires manual conversion: does not exist in peer-type
// WARNING: in.UID requires manual conversion: does not exist in peer-type
// WARNING: in.ResourceVersion requires manual conversion: does not exist in peer-type
// WARNING: in.Generation requires manual conversion: does not exist in peer-type
// WARNING: in.CreationTimestamp requires manual conversion: does not exist in peer-type
// WARNING: in.DeletionTimestamp requires manual conversion: does not exist in peer-type
// WARNING: in.DeletionGracePeriodSeconds requires manual conversion: does not exist in peer-type
out.Labels = *(*map[string]string)(unsafe.Pointer(&in.Labels))
out.Annotations = *(*map[string]string)(unsafe.Pointer(&in.Annotations))
// WARNING: in.OwnerReferences requires manual conversion: does not exist in peer-type
// WARNING: in.Finalizers requires manual conversion: does not exist in peer-type
// WARNING: in.ManagedFields requires manual conversion: does not exist in peer-type
return nil
}

func Convert_metav1_ObjectMeta_To_v1_ObjectMeta(in *metav1.ObjectMeta, out *ObjectMeta, s conversion.Scope) error {
return autoConvert_metav1_ObjectMeta_To_v1_ObjectMeta(in, out, s)
}

func Convert_v1_PodSpec_To_v2_PodSpec(in *PodSpec, out *v2.PodSpec, s conversion.Scope) error {
if in.Volumes != nil {
out.Volumes = in.Volumes
} else {
out.Volumes = nil
}
out.InitContainers = *(*[]core.Container)(unsafe.Pointer(&in.InitContainers))
out.TerminationGracePeriodSeconds = (*int64)(unsafe.Pointer(in.TerminationGracePeriodSeconds))
out.DNSPolicy = core.DNSPolicy(in.DNSPolicy)
out.NodeSelector = *(*map[string]string)(unsafe.Pointer(&in.NodeSelector))
out.ServiceAccountName = in.ServiceAccountName
out.HostNetwork = in.HostNetwork
out.HostPID = in.HostPID
out.HostIPC = in.HostIPC
out.ShareProcessNamespace = (*bool)(unsafe.Pointer(in.ShareProcessNamespace))
out.SecurityContext = (*core.PodSecurityContext)(unsafe.Pointer(in.SecurityContext))
out.ImagePullSecrets = *(*[]core.LocalObjectReference)(unsafe.Pointer(&in.ImagePullSecrets))
// WARNING: in.Affinity requires manual conversion: does not exist in peer-type
out.SchedulerName = in.SchedulerName
out.Tolerations = *(*[]core.Toleration)(unsafe.Pointer(&in.Tolerations))
out.PriorityClassName = in.PriorityClassName
out.Priority = (*int32)(unsafe.Pointer(in.Priority))
out.DNSConfig = (*core.PodDNSConfig)(unsafe.Pointer(in.DNSConfig))
out.RuntimeClassName = (*string)(unsafe.Pointer(in.RuntimeClassName))
out.EnableServiceLinks = (*bool)(unsafe.Pointer(in.EnableServiceLinks))
// WARNING: in.TopologySpreadConstraints requires manual conversion: does not exist in peer-type
// manual
out.Containers = []core.Container{
{
Name: "db",
Args: *(*[]string)(unsafe.Pointer(&in.Args)),
Env: *(*[]core.EnvVar)(unsafe.Pointer(&in.Env)),
Resources: in.Resources,
LivenessProbe: (*core.Probe)(unsafe.Pointer(in.LivenessProbe)),
ReadinessProbe: (*core.Probe)(unsafe.Pointer(in.ReadinessProbe)),
Lifecycle: (*core.Lifecycle)(unsafe.Pointer(in.Lifecycle)),
SecurityContext: (*core.SecurityContext)(unsafe.Pointer(in.ContainerSecurityContext)),
VolumeMounts: *(*[]core.VolumeMount)(unsafe.Pointer(&in.VolumeMounts)),
},
}
return nil
}

func Convert_v2_PodSpec_To_v1_PodSpec(in *v2.PodSpec, out *PodSpec, s conversion.Scope) error {
if in.Volumes != nil {
out.Volumes = in.Volumes
} else {
out.Volumes = nil
}
out.InitContainers = *(*[]core.Container)(unsafe.Pointer(&in.InitContainers))
// WARNING: in.Containers requires manual conversion: does not exist in peer-type
// WARNING: in.EphemeralContainers requires manual conversion: does not exist in peer-type
// WARNING: in.RestartPolicy requires manual conversion: does not exist in peer-type
out.TerminationGracePeriodSeconds = (*int64)(unsafe.Pointer(in.TerminationGracePeriodSeconds))
// WARNING: in.ActiveDeadlineSeconds requires manual conversion: does not exist in peer-type
out.DNSPolicy = core.DNSPolicy(in.DNSPolicy)
out.NodeSelector = *(*map[string]string)(unsafe.Pointer(&in.NodeSelector))
out.ServiceAccountName = in.ServiceAccountName
// WARNING: in.AutomountServiceAccountToken requires manual conversion: does not exist in peer-type
// WARNING: in.NodeName requires manual conversion: does not exist in peer-type
out.HostNetwork = in.HostNetwork
out.HostPID = in.HostPID
out.HostIPC = in.HostIPC
out.ShareProcessNamespace = (*bool)(unsafe.Pointer(in.ShareProcessNamespace))
out.SecurityContext = (*core.PodSecurityContext)(unsafe.Pointer(in.SecurityContext))
out.ImagePullSecrets = *(*[]core.LocalObjectReference)(unsafe.Pointer(&in.ImagePullSecrets))
out.SchedulerName = in.SchedulerName
out.Tolerations = *(*[]core.Toleration)(unsafe.Pointer(&in.Tolerations))
// WARNING: in.HostAliases requires manual conversion: does not exist in peer-type
out.PriorityClassName = in.PriorityClassName
out.Priority = (*int32)(unsafe.Pointer(in.Priority))
out.DNSConfig = (*core.PodDNSConfig)(unsafe.Pointer(in.DNSConfig))
// WARNING: in.ReadinessGates requires manual conversion: does not exist in peer-type
out.RuntimeClassName = (*string)(unsafe.Pointer(in.RuntimeClassName))
out.EnableServiceLinks = (*bool)(unsafe.Pointer(in.EnableServiceLinks))
// WARNING: in.PreemptionPolicy requires manual conversion: does not exist in peer-type
// WARNING: in.Overhead requires manual conversion: does not exist in peer-type
// WARNING: in.SetHostnameAsFQDN requires manual conversion: does not exist in peer-type
// WARNING: in.OS requires manual conversion: does not exist in peer-type
// WARNING: in.HostUsers requires manual conversion: does not exist in peer-type
if len(in.Containers) > 0 {
c := in.Containers[0]
out.Args = *(*[]string)(unsafe.Pointer(&c.Args))
out.Env = *(*[]core.EnvVar)(unsafe.Pointer(&c.Env))
out.Resources = c.Resources
out.LivenessProbe = (*core.Probe)(unsafe.Pointer(c.LivenessProbe))
out.ReadinessProbe = (*core.Probe)(unsafe.Pointer(c.ReadinessProbe))
out.Lifecycle = (*core.Lifecycle)(unsafe.Pointer(c.Lifecycle))
out.ContainerSecurityContext = (*core.SecurityContext)(unsafe.Pointer(c.SecurityContext))
out.VolumeMounts = *(*[]core.VolumeMount)(unsafe.Pointer(&c.VolumeMounts))
}
return nil
}

func autoConvert_v1_PodTemplateSpec_To_v2_PodTemplateSpec(in *PodTemplateSpec, out *v2.PodTemplateSpec, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
out.Controller = in.Controller
if err := Convert_v1_PodSpec_To_v2_PodSpec(&in.Spec, &out.Spec, s); err != nil {
return err
}
return nil
}

// Convert_v1_PodTemplateSpec_To_v2_PodTemplateSpec is an autogenerated conversion function.
func Convert_v1_PodTemplateSpec_To_v2_PodTemplateSpec(in *PodTemplateSpec, out *v2.PodTemplateSpec, s conversion.Scope) error {
return autoConvert_v1_PodTemplateSpec_To_v2_PodTemplateSpec(in, out, s)
}

func autoConvert_v2_PodTemplateSpec_To_v1_PodTemplateSpec(in *v2.PodTemplateSpec, out *PodTemplateSpec, s conversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
out.Controller = in.Controller
if err := Convert_v2_PodSpec_To_v1_PodSpec(&in.Spec, &out.Spec, s); err != nil {
return err
}
return nil
}

// Convert_v2_PodTemplateSpec_To_v1_PodTemplateSpec is an autogenerated conversion function.
func Convert_v2_PodTemplateSpec_To_v1_PodTemplateSpec(in *v2.PodTemplateSpec, out *PodTemplateSpec, s conversion.Scope) error {
return autoConvert_v2_PodTemplateSpec_To_v1_PodTemplateSpec(in, out, s)
}
1 change: 1 addition & 0 deletions api/v1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

// +k8s:deepcopy-gen=package
// +k8s:conversion-gen=kmodules.xyz/offshoot-api/api/v2
// +k8s:openapi-gen=true
// +gencrdrefdocs:force=true
package v1 // import "kmodules.xyz/offshoot-api/api/v1"
29 changes: 29 additions & 0 deletions api/v1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright AppsCode Inc. and Contributors
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 (
"k8s.io/apimachinery/pkg/runtime"
)

var (
// TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api.
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder
AddToScheme = localSchemeBuilder.AddToScheme
)
4 changes: 0 additions & 4 deletions api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,16 @@ type PodSpec struct {
// Host networking requested for this pod. Use the host's network namespace.
// If this option is set, the ports that will be used must be specified.
// Default to false.
// +k8s:conversion-gen=false
// +optional
HostNetwork bool `json:"hostNetwork,omitempty"`

// Use the host's pid namespace.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
HostPID bool `json:"hostPID,omitempty"`

// Use the host's ipc namespace.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
HostIPC bool `json:"hostIPC,omitempty"`

Expand All @@ -137,7 +134,6 @@ type PodSpec struct {
// in the same pod, and the first process in each container will not be assigned PID 1.
// HostPID and ShareProcessNamespace cannot both be set.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
ShareProcessNamespace *bool `json:"shareProcessNamespace,omitempty"`

Expand Down
5 changes: 0 additions & 5 deletions api/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,21 @@ type PodSpec struct {
// Host networking requested for this pod. Use the host's network namespace.
// If this option is set, the ports that will be used must be specified.
// Default to false.
// +k8s:conversion-gen=false
// +optional
HostNetwork bool `json:"hostNetwork,omitempty"`
// Use the host's pid namespace.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
HostPID bool `json:"hostPID,omitempty"`
// Use the host's ipc namespace.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
HostIPC bool `json:"hostIPC,omitempty"`
// Share a single process namespace between all of the containers in a pod.
// When this is set containers will be able to view and signal processes from other containers
// in the same pod, and the first process in each container will not be assigned PID 1.
// HostPID and ShareProcessNamespace cannot both be set.
// Optional: Default to false.
// +k8s:conversion-gen=false
// +optional
ShareProcessNamespace *bool `json:"shareProcessNamespace,omitempty"`
// SecurityContext holds pod-level security attributes and common container settings.
Expand Down Expand Up @@ -273,7 +269,6 @@ type PodSpec struct {
// mitigating container breakout vulnerabilities even allowing users to run their
// containers as root without actually having root privileges on the host.
// This field is alpha-level and is only honored by servers that enable the UserNamespacesSupport feature.
// +k8s:conversion-gen=false
// +optional
HostUsers *bool `json:"hostUsers,omitempty"`
}
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ require (
kmodules.xyz/client-go v0.29.13
)

require (
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand All @@ -32,7 +25,9 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
Expand All @@ -44,6 +39,8 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
Expand Down

0 comments on commit fd54099

Please sign in to comment.