Skip to content

Commit

Permalink
Support specify annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ciiiii committed Nov 22, 2024
1 parent 1c2f838 commit f4b02a7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
9 changes: 9 additions & 0 deletions cloud/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cloud

func convertToStringMap(m map[string]interface{}) map[string]string {
result := make(map[string]string)
for k, v := range m {
result[k] = v.(string)
}
return result
}
1 change: 1 addition & 0 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func init() {
"and the aliases of PrivateLinkService in Azure.",
"oauth2_issuer_url": "The issuer url of the oauth2",
"oauth2_audience": "The audience of the oauth2",
"annotations": "The metadata annotations of the resource",
}
}

Expand Down
27 changes: 19 additions & 8 deletions cloud/resource_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
)

func resourceCloudEnvironment() *schema.Resource {
Expand Down Expand Up @@ -157,6 +157,13 @@ func resourceCloudEnvironment() *schema.Resource {
},
},
},
"annotations": {
Type: schema.TypeMap,
Description: descriptions["annotations"],
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validateAnnotations,
},
"wait_for_completion": {
Type: schema.TypeBool,
Optional: true,
Expand All @@ -178,23 +185,27 @@ func resourceCloudEnvironmentCreate(ctx context.Context, d *schema.ResourceData,
zone := d.Get("zone").(string)
cloudConnectionName := d.Get("cloud_connection_name").(string)
network := d.Get("network").([]interface{})
rawAnnotations := d.Get("annotations").(map[string]interface{})
waitForCompletion := d.Get("wait_for_completion")

clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_CLOUD_ENVIRONMENT: %w", err))
}
annotations := make(map[string]string)
if len(rawAnnotations) > 0 {
annotations = convertToStringMap(rawAnnotations)
}
annotations["cloud.streamnative.io/environment-type"] = cloudEnvironmentType

cloudEnvironment := &cloudv1alpha1.CloudEnvironment{
TypeMeta: metav1.TypeMeta{
Kind: "CloudEnvironment",
APIVersion: cloudv1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Annotations: map[string]string{
"cloud.streamnative.io/environment-type": cloudEnvironmentType,
},
Namespace: namespace,
Annotations: annotations,
},
Spec: cloudv1alpha1.CloudEnvironmentSpec{
CloudConnectionName: cloudConnectionName,
Expand Down Expand Up @@ -407,7 +418,7 @@ func retryUntilCloudEnvironmentIsProvisioned(ctx context.Context, clientSet *clo
return func() *retry.RetryError {
ce, err := clientSet.CloudV1alpha1().CloudEnvironments(ns).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && errors.IsNotFound(statusErr) {
if statusErr, ok := err.(*apierrors.StatusError); ok && apierrors.IsNotFound(statusErr) {
return nil
}
return retry.NonRetryableError(err)
Expand Down
15 changes: 15 additions & 0 deletions cloud/validate_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"strconv"
"strings"

utilValidation "k8s.io/apimachinery/pkg/util/validation"
)

func validateNotBlank(val interface{}, key string) (warns []string, errs []error) {
Expand Down Expand Up @@ -125,6 +127,19 @@ func validateCidrRange(val interface{}, key string) (warns []string, errs []erro
return
}

func validateAnnotations(value interface{}, key string) (ws []string, es []error) {
m := value.(map[string]interface{})
for k := range m {
errors := utilValidation.IsQualifiedName(strings.ToLower(k))
if len(errors) > 0 {
for _, e := range errors {
es = append(es, fmt.Errorf("%s (%q) %s", key, k, e))
}
}
}
return
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
Expand Down

0 comments on commit f4b02a7

Please sign in to comment.