Skip to content

Commit cbf73e2

Browse files
committed
extend storageClassName to v1alpha1 annotation
1 parent 67ccd50 commit cbf73e2

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

pkg/webhook/admission/conversion.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package admission
33
import (
44
"encoding/json"
55
"fmt"
6+
"log"
67
"net/http"
8+
"strings"
79

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

35-
convertReview.Response = c.converter(convertReview.Request)
36-
w.WriteHeader(http.StatusOK)
37+
response, status := c.converter(convertReview.Request)
38+
convertReview.Response = response
39+
w.WriteHeader(status)
40+
if status == 200 {
41+
log.Printf("Successfully converted resource(s): %s", objectNames(convertReview))
42+
} else {
43+
log.Printf("Failed to convert resource(s) %s: %s", objectNames(convertReview), response.Result.Status)
44+
}
3745
b, _ := json.Marshal(convertReview)
3846
w.Write(b)
3947
}
4048

49+
func objectNames(review *apiextensionsv1.ConversionReview) string {
50+
names := []string{}
51+
for _, obj := range review.Response.ConvertedObjects {
52+
unstructured := unstructuredv1.Unstructured{}
53+
err := json.Unmarshal(obj.Raw, &unstructured)
54+
if err == nil {
55+
names = append(names, unstructured.GetName())
56+
}
57+
}
58+
return strings.Join(names, ",")
59+
}
60+
4161
// helper to construct error response.
42-
func errored(uid types.UID, err error) *apiextensionsv1.ConversionResponse {
62+
func errored(uid types.UID, err error) (*apiextensionsv1.ConversionResponse, int) {
4363
return &apiextensionsv1.ConversionResponse{
4464
UID: uid,
4565
Result: metav1.Status{
4666
Status: metav1.StatusFailure,
4767
Message: err.Error(),
4868
},
49-
}
69+
}, http.StatusBadRequest
5070
}
5171

5272
// Takes a conversionRequest and always returns a conversionResponse.
53-
func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest) *apiextensionsv1.ConversionResponse {
54-
73+
func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest) (*apiextensionsv1.ConversionResponse, int) {
5574
desiredAPIVersion := request.DesiredAPIVersion
5675
if desiredAPIVersion == "" {
5776
return errored(request.UID, fmt.Errorf("conversion request did not have a desired api version"))
5877
}
59-
6078
responseObjects := make([]runtime.RawExtension, len(request.Objects))
6179
for i, obj := range request.Objects {
6280
unstructured := unstructuredv1.Unstructured{}
@@ -109,5 +127,5 @@ func (c ConversionWebhook) converter(request *apiextensionsv1.ConversionRequest)
109127
Result: metav1.Status{
110128
Status: metav1.StatusSuccess,
111129
},
112-
}
130+
}, http.StatusOK
113131
}

pkg/webhook/admission/convert/v1alpha1_to_v1alpha2.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func ConvertV1alpha1ToV1alpha2(rawRequest []byte) ([]byte, runtime.Object, error
4343
want.Spec.OutputsToOmit = have.Spec.OutputsToOmit
4444
want.Spec.ServiceAccount = have.Spec.ServiceAccount
4545

46+
if storageClassName, ok := have.Annotations["v1alpha2.tf.isaaguilar.com/storageClassName"]; ok {
47+
want.Spec.StorageClassName = &storageClassName
48+
}
49+
4650
scriptImageConfig := convertImageConfig("script", have.Spec.ScriptRunner, have.Spec.ScriptRunnerVersion, have.Spec.ScriptRunnerPullPolicy)
4751
terraformImageConfig := convertImageConfig("terraform", have.Spec.TerraformRunner, "", have.Spec.TerraformRunnerPullPolicy)
4852
setupImageConfig := convertImageConfig("setup", have.Spec.SetupRunner, have.Spec.SetupRunnerVersion, have.Spec.SetupRunnerPullPolicy)

pkg/webhook/admission/convert/v1alpha2_to_v1alpha1.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ func ConvertV1alpha2ToV1alpha1(rawRequest []byte) ([]byte, runtime.Object, error
7676
want.Spec.OutputsToOmit = have.Spec.OutputsToOmit
7777
want.Spec.ServiceAccount = have.Spec.ServiceAccount
7878

79+
if have.Spec.StorageClassName != nil {
80+
if want.Annotations == nil {
81+
want.Annotations = map[string]string{}
82+
}
83+
want.Annotations["v1alpha2.tf.isaaguilar.com/storageClassName"] = *have.Spec.StorageClassName
84+
}
85+
7986
if have.Spec.Images != nil {
8087
if have.Spec.Images.Script != nil {
8188
image := ""

0 commit comments

Comments
 (0)