-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (139 loc) · 4.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v4/go/gcp/container"
"github.com/pulumi/pulumi-gcp/sdk/v4/go/gcp/projects"
appsv1 "github.com/pulumi/pulumi-kubernetes/sdk/v2/go/kubernetes/apps/v1"
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v2/go/kubernetes/core/v1"
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v2/go/kubernetes/meta/v1"
"github.com/pulumi/pulumi-kubernetes/sdk/v2/go/kubernetes/providers"
"github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
containerService, err := projects.NewService(ctx, "project", &projects.ServiceArgs{
Service: pulumi.String("container.googleapis.com"),
})
if err != nil {
return err
}
engineVersions, err := container.GetEngineVersions(ctx, &container.GetEngineVersionsArgs{})
if err != nil {
return err
}
masterVersion := engineVersions.LatestMasterVersion
cluster, err := container.NewCluster(ctx, "gke-playground-cluster", &container.ClusterArgs{
InitialNodeCount: pulumi.Int(2),
MinMasterVersion: pulumi.String(masterVersion),
NodeVersion: pulumi.String(masterVersion),
NodeConfig: &container.ClusterNodeConfigArgs{
MachineType: pulumi.String("n1-standard-1"),
OauthScopes: pulumi.StringArray{
pulumi.String("https://www.googleapis.com/auth/compute"),
pulumi.String("https://www.googleapis.com/auth/devstorage.read_only"),
pulumi.String("https://www.googleapis.com/auth/logging.write"),
pulumi.String("https://www.googleapis.com/auth/monitoring"),
},
},
}, pulumi.DependsOn([]pulumi.Resource{containerService}))
if err != nil {
return err
}
ctx.Export("kubeconfig", generateKubeconfig(cluster.Endpoint, cluster.Name, cluster.MasterAuth))
k8sProvider, err := providers.NewProvider(ctx, "k8sprovider", &providers.ProviderArgs{
Kubeconfig: generateKubeconfig(cluster.Endpoint, cluster.Name, cluster.MasterAuth),
}, pulumi.DependsOn([]pulumi.Resource{cluster}))
if err != nil {
return err
}
namespace, err := corev1.NewNamespace(ctx, "app-ns", &corev1.NamespaceArgs{
Metadata: &metav1.ObjectMetaArgs{
Name: pulumi.String("demo-ns"),
},
}, pulumi.Provider(k8sProvider))
if err != nil {
return err
}
appLabels := pulumi.StringMap{
"app": pulumi.String("demo-app"),
}
_, err = appsv1.NewDeployment(ctx, "app-dep", &appsv1.DeploymentArgs{
Metadata: &metav1.ObjectMetaArgs{
Namespace: namespace.Metadata.Elem().Name(),
},
Spec: appsv1.DeploymentSpecArgs{
Selector: &metav1.LabelSelectorArgs{
MatchLabels: appLabels,
},
Replicas: pulumi.Int(3),
Template: &corev1.PodTemplateSpecArgs{
Metadata: &metav1.ObjectMetaArgs{
Labels: appLabels,
},
Spec: &corev1.PodSpecArgs{
Containers: corev1.ContainerArray{
corev1.ContainerArgs{
Name: pulumi.String("demo-app"),
Image: pulumi.String("nginx:1.15-alpine"),
}},
},
},
},
}, pulumi.Provider(k8sProvider))
if err != nil {
return err
}
service, err := corev1.NewService(ctx, "app-service", &corev1.ServiceArgs{
Metadata: &metav1.ObjectMetaArgs{
Namespace: namespace.Metadata.Elem().Name(),
Labels: appLabels,
},
Spec: &corev1.ServiceSpecArgs{
Ports: corev1.ServicePortArray{
corev1.ServicePortArgs{
Port: pulumi.Int(80),
TargetPort: pulumi.Int(8080),
},
},
Selector: appLabels,
Type: pulumi.String("clusterIP"),
},
}, pulumi.Provider(k8sProvider))
if err != nil {
return err
}
ctx.Export("frontendIp", service.Spec.ApplyT(func(spec *corev1.ServiceSpec) *string {
return spec.ClusterIP
}))
return nil
})
}
func generateKubeconfig(clusterEndpoint pulumi.StringOutput, clusterName pulumi.StringOutput,
clusterMasterAuth container.ClusterMasterAuthOutput) pulumi.StringOutput {
context := pulumi.Sprintf("demo_%s", clusterName)
return pulumi.Sprintf(`apiVersion: v1
clusters:
- cluster:
certificate-authority-data: %s
server: https://%s
name: %s
contexts:
- context:
cluster: %s
user: %s
name: %s
current-context: %s
kind: Config
preferences: {}
users:
- name: %s
user:
auth-provider:
config:
cmd-args: config config-helper --format=json
cmd-path: gcloud
expiry-key: '{.credential.token_expiry}'
token-key: '{.credential.access_token}'
name: gcp`,
clusterMasterAuth.ClusterCaCertificate().Elem(),
clusterEndpoint, context, context, context, context, context, context)
}