square-night-79134
06/22/2023, 4:02 PMsalmon-account-74572
06/22/2023, 4:09 PMGet*
functions are named Lookup*
instead. So, rather than using container.GetCluster
you’d use container.LookupCluster
. Instead of using &container.GetClusterArgs
you’d use &container.LookupClusterArgs
.
This page has an example (be sure to click the Go language selector): https://www.pulumi.com/registry/packages/gcp/api-docs/container/getcluster/square-night-79134
06/22/2023, 4:16 PMnewCluster, err := container.NewCluster(ctx, "new-cluster", &container.ClusterArgs{
AddonsConfig: existingCluster.AddonsConfig,
ClusterAutoscaling: existingCluster.ClusterAutoscaling,
DefaultSnatStatus: existingCluster.DefaultSnatStatus,
EnableKubernetesAlpha: existingCluster.EnableKubernetesAlpha,
EnableTpu: existingCluster.EnableTpu,
IpAllocationPolicy: existingCluster.IpAllocationPolicy,
Location: pulumi.String("your-new-cluster-location"),
LoggingService: existingCluster.LoggingService,
MaintenancePolicy: existingCluster.MaintenancePolicy,
NetworkPolicy: existingCluster.NetworkPolicy,
PodSecurityPolicyConfig: existingCluster.PodSecurityPolicyConfig,
PrivateClusterConfig: existingCluster.PrivateClusterConfig,
Project: newProjectID,
ResourceLabels: existingCluster.ResourceLabels,
ResourceUsageExportConfig: existingCluster.ResourceUsageExportConfig,
})
salmon-account-74572
06/22/2023, 4:21 PMkubernetes-gcp-go
template (run pulumi new kubernetes-gcp-go
and review the program created from it). Then compare that against what is already there.square-night-79134
06/22/2023, 4:25 PMpackage main
import (
"<http://github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/container|github.com/pulumi/pulumi-gcp/sdk/v6/go/gcp/container>"
"<http://github.com/pulumi/pulumi/sdk/v3/go/pulumi|github.com/pulumi/pulumi/sdk/v3/go/pulumi>"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Get existing GKE cluster
existingCluster, err := container.LookupCluster(ctx, &container.LookupClusterArgs{
Location: pulumi.StringRef("your-existing-cluster-location"),
Name: "your-existing-cluster-name",
Project: pulumi.StringRef("your-existing-project-id"),
})
if err != nil {
return err
}
// Change "your-new-project-id" to the new project ID where you want to create the cluster
newProjectID := pulumi.String("your-new-project-id")
// Create a new GKE cluster with the same settings in a different project
newCluster, err := container.NewCluster(ctx, "new-cluster", &container.ClusterArgs{
AddonsConfig: existingCluster.AddonsConfig,
ClusterAutoscaling: existingCluster.ClusterAutoscaling,
DefaultSnatStatus: existingCluster.DefaultSnatStatus,
EnableKubernetesAlpha: existingCluster.EnableKubernetesAlpha,
EnableTpu: existingCluster.EnableTpu,
IpAllocationPolicy: existingCluster.IpAllocationPolicy,
Location: pulumi.String("your-new-cluster-location"),
LoggingService: existingCluster.LoggingService,
MaintenancePolicy: existingCluster.MaintenancePolicy,
NetworkPolicy: existingCluster.NetworkPolicy,
PodSecurityPolicyConfig: existingCluster.PodSecurityPolicyConfig,
PrivateClusterConfig: existingCluster.PrivateClusterConfig,
Project: newProjectID,
ResourceLabels: existingCluster.ResourceLabels,
ResourceUsageExportConfig: existingCluster.ResourceUsageExportConfig,
})
if err != nil {
return err
}
ctx.Export("newClusterName", newCluster.Name)
ctx.Export("ClusterEndpoint", newCluster.Endpoint)
return nil
})
}
salmon-account-74572
06/22/2023, 4:34 PMLookupCluster
to feed information into NewCluster
is supported/feasible. My IDE definitely doesn’t like your code; it’s throwing all kinds of type errors.square-night-79134
06/22/2023, 4:37 PM// NewCluster registers a new resource with the given unique name, arguments, and options.
func NewCluster(ctx *pulumi.Context,
name string, args *ClusterArgs, opts ...pulumi.ResourceOption) (*Cluster, error) {
if args == nil {
args = &ClusterArgs{}
}
var resource Cluster
err := ctx.RegisterResource("gcp:container/cluster:Cluster", name, args, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}
// GetCluster gets an existing Cluster resource's state with the given name, ID, and optional
// state properties that are used to uniquely qualify the lookup (nil if not required).
func GetCluster(ctx *pulumi.Context,
name string, id pulumi.IDInput, state *ClusterState, opts ...pulumi.ResourceOption) (*Cluster, error) {
var resource Cluster
err := ctx.ReadResource("gcp:container/cluster:Cluster", name, id, state, &resource, opts...)
if err != nil {
return nil, err
}
return &resource, nil
}
salmon-account-74572
06/22/2023, 5:52 PMkubernetes-gcp-go
template and iterate on that to create a matching resource.square-night-79134
06/23/2023, 7:33 AMsalmon-account-74572
06/23/2023, 2:22 PMpulumi import
should generate code for you, and you can then duplicate that code to create another resource. But as someone else pointed out in another thread, with an Autopilot cluster a lot of the settings won’t come in (because Autopilot handles all that).