Skip to content

Commit

Permalink
Merge branch 'main' into mh/71
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch-hamm authored Nov 15, 2024
2 parents 19f4ea8 + bee665c commit ae44d3d
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cloud/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var apiKeyGeneratedName = fmt.Sprintf("e2e-%d", rand.Intn(10000))
var apiKeyGeneratedName = fmt.Sprintf("ak-%d", rand.Intn(1000))

func TestApiKey(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwe"
"github.com/streamnative/terraform-provider-streamnative/cloud/util"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
Expand Down Expand Up @@ -119,6 +120,10 @@ func DataSourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
}
apiKey, err := clientSet.CloudV1alpha1().APIKeys(organization).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_API_KEY: %w", err))
}
if err = d.Set("organization", apiKey.Namespace); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -120,6 +121,10 @@ func dataSourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData,
}
cloudConnection, err := clientSet.CloudV1alpha1().CloudConnections(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -131,6 +132,10 @@ func dataSourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData,
}
cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -57,6 +58,10 @@ func DataSourcePoolRead(ctx context.Context, d *schema.ResourceData, meta interf
}
pool, err := clientSet.CloudV1alpha1().Pools(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_POOL: %w", err))
}
_ = d.Set("name", pool.Name)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pool_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -69,6 +70,10 @@ func DataSourcePoolMemberRead(ctx context.Context, d *schema.ResourceData, meta
}
poolMember, err := clientSet.CloudV1alpha1().PoolMembers(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_POOL_MEMBER: %w", err))
}
_ = d.Set("name", poolMember.Name)
Expand Down
7 changes: 7 additions & 0 deletions cloud/data_source_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -27,6 +28,8 @@ import (

const (
IstioEnabledAnnotation = "annotations.cloud.streamnative.io/istio-enabled"
UrsaEngineAnnotation = "cloud.streamnative.io/engine"
UrsaEngineValue = "ursa"
)

func dataSourcePulsarCluster() *schema.Resource {
Expand Down Expand Up @@ -259,6 +262,10 @@ func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, me
}
pulsarCluster, err := clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
}
_ = d.Set("ready", "False")
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pulsar_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -100,6 +101,10 @@ func dataSourcePulsarGatewayRead(ctx context.Context, d *schema.ResourceData, me
}
pg, err := clientSet.CloudV1alpha1().PulsarGateways(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_GATEWAY: %w", err))
}
d.Set("access", pg.Spec.Access)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pulsar_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -98,6 +99,10 @@ func dataSourcePulsarInstanceRead(ctx context.Context, d *schema.ResourceData, m
}
pulsarInstance, err := clientSet.CloudV1alpha1().PulsarInstances(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_INSTANCE: %w", err))
}
_ = d.Set("ready", "False")
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -76,6 +77,10 @@ func DataSourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, m
}
serviceAccount, err := clientSet.CloudV1alpha1().ServiceAccounts(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT: %w", err))
}
_ = d.Set("name", serviceAccount.Name)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_service_account_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -81,6 +82,10 @@ func DataSourceServiceAccountBindingRead(ctx context.Context, d *schema.Resource
}
serviceAccountBinding, err := clientSet.CloudV1alpha1().ServiceAccountBindings(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT_BINDING: %w", err))
}
_ = d.Set("name", serviceAccountBinding.Name)
Expand Down
1 change: 1 addition & 0 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func init() {
"pool_member_location": "The location of the infrastructure pool member",
"instance_name": "The pulsar instance name",
"instance_type": "The streamnative cloud instance type, supporting 'serverless' and 'standard'",
"instance_engine": "The streamnative cloud instance engine, supporting 'ursa' and 'classic', default 'classic'",
"location": "The location of the pulsar cluster, " +
"supported location https://docs.streamnative.io/docs/cluster#cluster-location",
"release_channel": "The release channel of the pulsar cluster subscribe to, it must to be lts or rapid, default rapid",
Expand Down
2 changes: 1 addition & 1 deletion cloud/pulsar_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var clusterGeneratedName = fmt.Sprintf("terraform-test-cluster-%d-%d", rand.Intn(10000), rand.Intn(10000))
var clusterGeneratedName = fmt.Sprintf("t-%d-%d", rand.Intn(1000), rand.Intn(100))

func TestPulsarCluster(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down
12 changes: 9 additions & 3 deletions cloud/resource_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strings"
"time"

"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"
"github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
"github.com/streamnative/terraform-provider-streamnative/cloud/util"
"github.com/xhit/go-str2duration/v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"strings"
"time"
)

func resourceApiKey() *schema.Resource {
Expand Down Expand Up @@ -310,6 +312,10 @@ func resourceApiKeyRead(ctx context.Context, d *schema.ResourceData, m interface
}
apiKey, err := clientSet.CloudV1alpha1().APIKeys(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_API_KEY: %w", err))
}
if err = d.Set("organization", apiKey.Namespace); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cloud/resource_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"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"
Expand Down Expand Up @@ -251,6 +253,10 @@ func resourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData, me
}
cloudConnection, err := clientSet.CloudV1alpha1().CloudConnections(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/resource_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
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"
)

Expand Down Expand Up @@ -266,6 +267,10 @@ func resourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData, m
}
cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
}

Expand Down
23 changes: 20 additions & 3 deletions cloud/resource_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package cloud
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"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"
Expand Down Expand Up @@ -350,6 +352,8 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
"creating a cluster under instance of type '%s' is no longer allowed",
cloudv1alpha1.PulsarInstanceTypeFree))
}
ursaEngine, ok := pulsarInstance.Annotations[UrsaEngineAnnotation]
ursaEnabled := ok && ursaEngine == UrsaEngineValue
bookieCPU := resource.NewMilliQuantity(int64(storageUnit*2*1000), resource.DecimalSI)
brokerCPU := resource.NewMilliQuantity(int64(computeUnit*2*1000), resource.DecimalSI)
brokerMem := resource.NewQuantity(int64(computeUnit*8*1024*1024*1024), resource.DecimalSI)
Expand Down Expand Up @@ -410,6 +414,15 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
"cloud.streamnative.io/type": "serverless",
}
}
if ursaEnabled {
if pulsarCluster.Annotations == nil {
pulsarCluster.Annotations = map[string]string{
UrsaEngineAnnotation: UrsaEngineValue,
}
} else {
pulsarCluster.Annotations[UrsaEngineAnnotation] = UrsaEngineValue
}
}
if displayName == "" && name == "" {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_PULSAR_CLUSTER: " +
"either name or display_name must be provided"))
Expand Down Expand Up @@ -453,7 +466,7 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
return resourcePulsarClusterRead(ctx, d, meta)
}
}
err = retry.RetryContext(ctx, 15*time.Minute, func() *retry.RetryError {
err = retry.RetryContext(ctx, 20*time.Minute, func() *retry.RetryError {
dia := resourcePulsarClusterRead(ctx, d, meta)
if dia.HasError() {
return retry.NonRetryableError(fmt.Errorf("ERROR_RETRY_READ_PULSAR_CLUSTER: %s", dia[0].Summary))
Expand Down Expand Up @@ -482,6 +495,10 @@ func resourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta
if name != "" {
pulsarCluster, err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
}
} else {
Expand Down Expand Up @@ -665,7 +682,7 @@ func resourcePulsarClusterUpdate(ctx context.Context, d *schema.ResourceData, me
}
// Delay 10 seconds to wait for api server start reconcile.
time.Sleep(10 * time.Second)
err = retry.RetryContext(ctx, 15*time.Minute, func() *retry.RetryError {
err = retry.RetryContext(ctx, 20*time.Minute, func() *retry.RetryError {
dia := resourcePulsarClusterRead(ctx, d, meta)
if dia.HasError() {
return retry.NonRetryableError(fmt.Errorf("ERROR_RETRY_READ_PULSAR_CLUSTER: %s", dia[0].Summary))
Expand Down
4 changes: 4 additions & 0 deletions cloud/resource_pulsar_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func resourcePulsarGatewayRead(ctx context.Context, d *schema.ResourceData, meta

pg, err := clientSet.CloudV1alpha1().PulsarGateways(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_GATEWAY: %w", err))
}
d.SetId(fmt.Sprintf("%s/%s", pg.Namespace, pg.Name))
Expand Down
Loading

0 comments on commit ae44d3d

Please sign in to comment.