-
Notifications
You must be signed in to change notification settings - Fork 880
/
main.go
95 lines (87 loc) · 2.54 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
package main
import (
appsv1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/apps/v1"
corev1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/core/v1"
metav1 "github.com/pulumi/pulumi-kubernetes/sdk/v3/go/kubernetes/meta/v1"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Minikube does not implement services of type `LoadBalancer`; require the user to specify if we're
// running on minikube, and if so, create only services of type ClusterIP.
config := config.New(ctx, "")
isMinikube := config.Require("isMinikube")
// nginx container, replicated 1 time
appName := "nginx"
appLabels := pulumi.StringMap{
"app": pulumi.String(appName),
}
_, err := appsv1.NewDeployment(ctx, "nginx", &appsv1.DeploymentArgs{
// _, err := appsv1.NewDeployment(ctx, "nginx", &appsv1.DeploymentArgs{
Spec: appsv1.DeploymentSpecArgs{
Selector: &metav1.LabelSelectorArgs{
MatchLabels: appLabels,
},
Replicas: pulumi.Int(1),
Template: &corev1.PodTemplateSpecArgs{
Metadata: &metav1.ObjectMetaArgs{
Labels: appLabels,
},
Spec: &corev1.PodSpecArgs{
Containers: corev1.ContainerArray{
corev1.ContainerArgs{
Name: pulumi.String(appName),
Image: pulumi.String("nginx:1.15-alpine"),
},
},
},
},
},
})
if err != nil {
return err
}
// Allocate an IP to the nginx deployment.
var frontendServiceType string
if isMinikube == "true" {
frontendServiceType = "ClusterIP"
} else {
frontendServiceType = "LoadBalancer"
}
frontend, err := corev1.NewService(ctx, appName, &corev1.ServiceArgs{
Metadata: &metav1.ObjectMetaArgs{
Labels: appLabels,
},
Spec: &corev1.ServiceSpecArgs{
Type: pulumi.String(frontendServiceType),
Ports: &corev1.ServicePortArray{
corev1.ServicePortArgs{
Port: pulumi.Int(80),
TargetPort: pulumi.Int(80),
Protocol: pulumi.String("TCP"),
},
},
Selector: appLabels,
},
})
if err != nil {
return err
}
// Export the public IP
if isMinikube == "true" {
ctx.Export("frontendIp", frontend.Spec.ApplyT(func(spec *corev1.ServiceSpec) *string {
return spec.ClusterIP
}))
} else {
ctx.Export("frontendIp", frontend.Status.ApplyT(func(status *corev1.ServiceStatus) *string {
ingress := status.LoadBalancer.Ingress[0]
if ingress.Hostname != nil {
return ingress.Hostname
}
return ingress.Ip
}))
}
return nil
})
}