Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Convert to conversion webhook and implement Cluster conversion #2

Merged
merged 2 commits into from
Mar 25, 2021

Conversation

@tfussell tfussell self-assigned this Mar 2, 2021
@tfussell tfussell changed the base branch from main to init March 2, 2021 21:44
@tfussell tfussell changed the title Implement webhook structure Convert to conversion webhook and implement Cluster conversion Mar 3, 2021
)

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
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

Comment on lines +70 to +97
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
Copy link
Contributor Author

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.

Copy link

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 ?

Copy link
Contributor Author

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.

@tfussell
Copy link
Contributor Author

tfussell commented Mar 3, 2021

I tested this with some curls. It works for simple cases at least.

v1alpha3 => v1alpha4

curl -s -H "Content-Type: application/json" localhost:8443/crdconvert --data '{"kind":"ConversionReview","apiVersion":"apiextensions.k8s.io/v1","request":{"uid":"abc","desiredAPIVersion":"cluster.x-k8s.io/v1alpha4","objects":[{"kind":"Cluster","apiVersion":"cluster.x-k8s.io/v1alpha3","metadata":{"name":"def"},"spec":{"paused":true}}]}}' | jq .

{
  "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

curl -s -H "Content-Type: application/json" localhost:8443/crdconvert --data '{"kind":"ConversionReview","apiVersion":"apiextensions.k8s.io/v1","request":{"uid":"abc","desiredAPIVersion":"cluster.x-k8s.io/v1alpha3","objects":[{"kind":"Cluster","apiVersion":"cluster.x-k8s.io/v1alpha4","metadata":{"name":"def"},"spec":{"paused":true}}]}}' | jq .

{
  "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"
    }
  }
}

@tfussell tfussell marked this pull request as ready for review March 3, 2021 01:39
err = outSerializer.Encode(&v1.ConversionReview{
TypeMeta: metav1.TypeMeta{
Kind: "ConversionReview",
APIVersion: "apiextensions.k8s.io/v1",
Copy link
Contributor Author

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?

Base automatically changed from init to main March 3, 2021 16:12
@tfussell
Copy link
Contributor Author

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.

@tfussell tfussell merged commit e302dff into main Mar 25, 2021
@tfussell tfussell deleted the implement branch March 25, 2021 16:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants