You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I have implemented conversion webhook for my CRD per guide from kubebuilder book.
I have dedicated v1beta1 version as Hub and v1alpha1 version as Spoke. Below, you can find code for conversion between the versions:
api/v1beta1/teamcity_conversions.go
package v1beta1
func (*TeamCity) Hub() {}
api/v1alpha/teamcity_conversions.go
// ConvertTo converts this TeamCity to the Hub version (v1beta1).func (src*TeamCity) ConvertTo(dstRaw conversion.Hub) error {
dst:=dstRaw.(*v1beta1.TeamCity)
//version specific conversiondataDirIndex:=0dataDir:=src.Spec.PersistentVolumeClaims[dataDirIndex]
dst.Spec.DataDirVolumeClaim=v1beta1.CustomPersistentVolumeClaim(dataDir)
//remove data dir from list of pvcstrimmedPersistentVolumeClaims:=append(src.Spec.PersistentVolumeClaims[:dataDirIndex], src.Spec.PersistentVolumeClaims[dataDirIndex:]...)
//add other elements into a new typevarnewPersistentVolumeClaimList []v1beta1.CustomPersistentVolumeClaimfor_, element:=rangetrimmedPersistentVolumeClaims {
newPersistentVolumeClaimList=append(newPersistentVolumeClaimList, v1beta1.CustomPersistentVolumeClaim(element))
}
//save list of persistent volumes into a new objectdst.Spec.PersistentVolumeClaims=newPersistentVolumeClaimList//the rest fields that remain the same//object metadst.ObjectMeta=src.ObjectMeta//object specdst.Spec.Env=src.Spec.Envdst.Spec.Image=src.Spec.Imagedst.Spec.Requests=src.Spec.Requestsdst.Spec.Limits=src.Spec.Limitsdst.Spec.TeamCityServerPort=src.Spec.TeamCityServerPortdst.Spec.PodSecurityContext=src.Spec.PodSecurityContextdst.Spec.XmxPercentage=src.Spec.XmxPercentagedst.Spec.HealthEndpoint=src.Spec.HealthEndpointdst.Spec.ReadinessProbeSettings=src.Spec.ReadinessProbeSettingsdst.Spec.LivenessProbeSettings=src.Spec.LivenessProbeSettingsdst.Spec.StartupProbeSettings=src.Spec.StartupProbeSettingsdst.Spec.InitContainers=src.Spec.InitContainersdst.Spec.StartupPropertiesConfig=src.Spec.StartupPropertiesConfigdst.Spec.DatabaseSecret=v1beta1.DatabaseSecret(src.Spec.DatabaseSecret)
//object statusdst.Status=v1beta1.TeamCityStatus(src.Status)
returnnil
}
// ConvertFrom converts from the Hub version (v1beta1) to this version.func (dst*TeamCity) ConvertFrom(srcRaw conversion.Hub) error {
src:=srcRaw.(*v1beta1.TeamCity)
//version specific conversiondataDirPersistentVolumeClaim:=CustomPersistentVolumeClaim(src.Spec.DataDirVolumeClaim)
varpersistentVolumeClaimList []CustomPersistentVolumeClaimpersistentVolumeClaimList=append(persistentVolumeClaimList, dataDirPersistentVolumeClaim)
for_, elem:=rangesrc.Spec.PersistentVolumeClaims {
persistentVolumeClaimList=append(persistentVolumeClaimList, CustomPersistentVolumeClaim(elem))
}
//object metadst.ObjectMeta=src.ObjectMeta//object specdst.Spec.Env=src.Spec.Envdst.Spec.Image=src.Spec.Imagedst.Spec.Requests=src.Spec.Requestsdst.Spec.Limits=src.Spec.Limitsdst.Spec.TeamCityServerPort=src.Spec.TeamCityServerPortdst.Spec.PodSecurityContext=src.Spec.PodSecurityContextdst.Spec.XmxPercentage=src.Spec.XmxPercentagedst.Spec.HealthEndpoint=src.Spec.HealthEndpointdst.Spec.ReadinessProbeSettings=src.Spec.ReadinessProbeSettingsdst.Spec.LivenessProbeSettings=src.Spec.LivenessProbeSettingsdst.Spec.StartupProbeSettings=src.Spec.StartupProbeSettingsdst.Spec.InitContainers=src.Spec.InitContainersdst.Spec.StartupPropertiesConfig=src.Spec.StartupPropertiesConfigdst.Spec.DatabaseSecret=DatabaseSecret(src.Spec.DatabaseSecret)
dst.Status=TeamCityStatus(src.Status)
returnnil
}
I have also added +kubebuilder:storageversion annotation in v1beta1 type. In the generated CRD, storage: true is set for v1beta1 version and storage: false for v1alpha1 version.
I believe my only deviation from the tutorial is that I use kustomize to patch CRD and Webhooks configuration to be able to run operator with webhooks locally as was suggested here - #400 (comment)
In the end, my conversion field in CRD looks like this:
When I try to apply object with version v1beta1, reconciliation is invoked and all is well.
However, when I try to apply object of v1alpha1 version, I can see that first ConvertTo function is invoked(which is expected) and ConversionResponse is returned with a valid object of version v1beta1. No error occurs. Immediately after, ConvertFrom function is invoked which is not expected. That indicates that for some reason conversion from v1beta1 to v1alpha1 is happening. That I cannot exactly explain. Once ConversionResponse is sent with a valid version v1alpha1, nothing happens - no custome object is created(neither v1beta1 nor v1alpha1).
I have inspected kube-api logs and could not find anything.
I have checked for similar issues, but could only find #2109. This does not seems to be my case because conversions back and forth is not repeated a few times. Therefore, I am looking for guidance on this matter.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I have implemented conversion webhook for my CRD per guide from kubebuilder book.
I have dedicated v1beta1 version as Hub and v1alpha1 version as Spoke. Below, you can find code for conversion between the versions:
api/v1beta1/teamcity_conversions.go
api/v1alpha/teamcity_conversions.go
I have also added
+kubebuilder:storageversion
annotation in v1beta1 type. In the generated CRD,storage: true
is set for v1beta1 version andstorage: false
for v1alpha1 version.I believe my only deviation from the tutorial is that I use kustomize to patch CRD and Webhooks configuration to be able to run operator with webhooks locally as was suggested here - #400 (comment)
In the end, my conversion field in CRD looks like this:
Observed behaviour:
When I try to apply object with version v1beta1, reconciliation is invoked and all is well.
However, when I try to apply object of v1alpha1 version, I can see that first ConvertTo function is invoked(which is expected) and ConversionResponse is returned with a valid object of version v1beta1. No error occurs. Immediately after, ConvertFrom function is invoked which is not expected. That indicates that for some reason conversion from v1beta1 to v1alpha1 is happening. That I cannot exactly explain. Once ConversionResponse is sent with a valid version v1alpha1, nothing happens - no custome object is created(neither v1beta1 nor v1alpha1).
I have inspected kube-api logs and could not find anything.
I have checked for similar issues, but could only find #2109. This does not seems to be my case because conversions back and forth is not repeated a few times. Therefore, I am looking for guidance on this matter.
Kind regards,
Anton
Beta Was this translation helpful? Give feedback.
All reactions