Skip to content

Commit

Permalink
extend storageClassName to v1alpha1 annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaguilar committed May 24, 2023
1 parent 67ccd50 commit cbf73e2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pkg/webhook/admission/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package admission
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"

"github.com/go-logr/logr"
"github.com/isaaguilar/terraform-operator/pkg/webhook/admission/convert"
Expand Down Expand Up @@ -32,31 +34,47 @@ func (c ConversionWebhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

convertReview.Response = c.converter(convertReview.Request)
w.WriteHeader(http.StatusOK)
response, status := c.converter(convertReview.Request)
convertReview.Response = response
w.WriteHeader(status)
if status == 200 {
log.Printf("Successfully converted resource(s): %s", objectNames(convertReview))
} else {
log.Printf("Failed to convert resource(s) %s: %s", objectNames(convertReview), response.Result.Status)
}
b, _ := json.Marshal(convertReview)
w.Write(b)
}

func objectNames(review *apiextensionsv1.ConversionReview) string {
names := []string{}
for _, obj := range review.Response.ConvertedObjects {
unstructured := unstructuredv1.Unstructured{}
err := json.Unmarshal(obj.Raw, &unstructured)
if err == nil {
names = append(names, unstructured.GetName())
}
}
return strings.Join(names, ",")
}

// helper to construct error response.
func errored(uid types.UID, err error) *apiextensionsv1.ConversionResponse {
func errored(uid types.UID, err error) (*apiextensionsv1.ConversionResponse, int) {
return &apiextensionsv1.ConversionResponse{
UID: uid,
Result: metav1.Status{
Status: metav1.StatusFailure,
Message: err.Error(),
},
}
}, http.StatusBadRequest
}

// Takes a conversionRequest and always returns a conversionResponse.
func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest) *apiextensionsv1.ConversionResponse {

func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest) (*apiextensionsv1.ConversionResponse, int) {
desiredAPIVersion := request.DesiredAPIVersion
if desiredAPIVersion == "" {
return errored(request.UID, fmt.Errorf("conversion request did not have a desired api version"))
}

responseObjects := make([]runtime.RawExtension, len(request.Objects))
for i, obj := range request.Objects {
unstructured := unstructuredv1.Unstructured{}
Expand Down Expand Up @@ -109,5 +127,5 @@ func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest)
Result: metav1.Status{
Status: metav1.StatusSuccess,
},
}
}, http.StatusOK
}
4 changes: 4 additions & 0 deletions pkg/webhook/admission/convert/v1alpha1_to_v1alpha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func ConvertV1alpha1ToV1alpha2(rawRequest []byte) ([]byte, runtime.Object, error
want.Spec.OutputsToOmit = have.Spec.OutputsToOmit
want.Spec.ServiceAccount = have.Spec.ServiceAccount

if storageClassName, ok := have.Annotations["v1alpha2.tf.isaaguilar.com/storageClassName"]; ok {
want.Spec.StorageClassName = &storageClassName
}

scriptImageConfig := convertImageConfig("script", have.Spec.ScriptRunner, have.Spec.ScriptRunnerVersion, have.Spec.ScriptRunnerPullPolicy)
terraformImageConfig := convertImageConfig("terraform", have.Spec.TerraformRunner, "", have.Spec.TerraformRunnerPullPolicy)
setupImageConfig := convertImageConfig("setup", have.Spec.SetupRunner, have.Spec.SetupRunnerVersion, have.Spec.SetupRunnerPullPolicy)
Expand Down
7 changes: 7 additions & 0 deletions pkg/webhook/admission/convert/v1alpha2_to_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func ConvertV1alpha2ToV1alpha1(rawRequest []byte) ([]byte, runtime.Object, error
want.Spec.OutputsToOmit = have.Spec.OutputsToOmit
want.Spec.ServiceAccount = have.Spec.ServiceAccount

if have.Spec.StorageClassName != nil {
if want.Annotations == nil {
want.Annotations = map[string]string{}
}
want.Annotations["v1alpha2.tf.isaaguilar.com/storageClassName"] = *have.Spec.StorageClassName
}

if have.Spec.Images != nil {
if have.Spec.Images.Script != nil {
image := ""
Expand Down

0 comments on commit cbf73e2

Please sign in to comment.