-
Notifications
You must be signed in to change notification settings - Fork 1
Convert to conversion webhook and implement Cluster conversion #2
Conversation
) | ||
|
||
replace sigs.k8s.io/cluster-api v0.3.13 => github.com/giantswarm/cluster-api v0.3.10-gs | ||
replace sigs.k8s.io/cluster-api => sigs.k8s.io/cluster-api v0.3.11-0.20210302171319-f7351b165992 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using CAPI master
branch for v1alpha4
.
go serveMetrics(config, metrics) | ||
serveTLS(config, handler) | ||
go serveMetrics(webhookConfig, metrics) | ||
http.ListenAndServe(":8443", handler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disabling TLS for testing temporarily. Need to reenable for Kubernetes to be able to talk to it.
case fromGVK == clusterV1Alpha4 && toVersion == capiV1Alpha3.String(): | ||
var fromCluster v1alpha4.Cluster | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(fromContent, &fromCluster) | ||
if err != nil { | ||
return nil, microerror.Mask(err) | ||
} | ||
|
||
toCluster := &v1alpha3.Cluster{} | ||
err = toCluster.ConvertFrom(&fromCluster) | ||
if err != nil { | ||
return nil, microerror.Mask(err) | ||
} | ||
|
||
result = toCluster | ||
case fromGVK == clusterV1Alpha3 && toVersion == capiV1Alpha4.String(): | ||
var fromCluster v1alpha3.Cluster | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(fromContent, &fromCluster) | ||
if err != nil { | ||
return nil, microerror.Mask(err) | ||
} | ||
|
||
toCluster := &v1alpha4.Cluster{} | ||
err = fromCluster.ConvertTo(toCluster) | ||
if err != nil { | ||
return nil, microerror.Mask(err) | ||
} | ||
|
||
result = toCluster |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the important part doing the actual conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember we talked about some generic object from kubebuilder to use for conversion from x to y. We don't do this at all or just don't do this in POC ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a go interface conversion.Hub
which v1alpha3 types implement. We can make the code use the spoke->hub->spoke model, but since there are only two versions I'm doing hub->spoke and spoke->hub directly. We can refactor as we add more types and versions.
I tested this with some v1alpha3 => v1alpha4
{
"kind": "ConversionReview",
"apiVersion": "apiextensions.k8s.io/v1",
"response": {
"uid": "abc",
"convertedObjects": [
{
"apiVersion": "cluster.x-k8s.io/v1alpha4",
"metadata": {
"creationTimestamp": null,
"name": "def"
},
"spec": {
"controlPlaneEndpoint": {
"host": "",
"port": 0
},
"paused": true
},
"status": {
"controlPlaneInitialized": false,
"infrastructureReady": false
}
}
],
"result": {
"metadata": {},
"status": "Success"
}
}
} v1alpha4 => v1alpha3
{
"kind": "ConversionReview",
"apiVersion": "apiextensions.k8s.io/v1",
"response": {
"uid": "abc",
"convertedObjects": [
{
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"metadata": {
"creationTimestamp": null,
"name": "def"
},
"spec": {
"controlPlaneEndpoint": {
"host": "",
"port": 0
},
"paused": true
},
"status": {
"controlPlaneInitialized": false,
"infrastructureReady": false
}
}
],
"result": {
"metadata": {},
"status": "Success"
}
}
} |
err = outSerializer.Encode(&v1.ConversionReview{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "ConversionReview", | ||
APIVersion: "apiextensions.k8s.io/v1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about this APIVersion
. Could someone confirm that this is correct?
I'm going to go ahead and merge this so it's not lost. I will probably deprecate the repo if it doesn't seem like we have a need for it in the near future. |
Based on example https://github.com/kubernetes/kubernetes/blob/v1.15.0/test/images/crd-conversion-webhook/main.go from docs here https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#webhook-conversion. Basically leveraging the existing conversion functions here https://github.com/kubernetes-sigs/cluster-api/blob/master/api/v1alpha3/conversion.go#L26-L30.