-
Notifications
You must be signed in to change notification settings - Fork 203
/
output.go
130 lines (114 loc) · 4.35 KB
/
output.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package engine
import (
"fmt"
"path"
"strconv"
"github.com/Azure/agentbaker/pkg/agent/datamodel"
"github.com/Azure/agentbaker/pkg/aks-engine/api"
"github.com/Azure/agentbaker/pkg/aks-engine/helpers"
"github.com/pkg/errors"
)
// ArtifactWriter represents the object that writes artifacts
type ArtifactWriter struct{}
// WriteTLSArtifacts saves TLS certificates and keys to the server filesystem
func (w *ArtifactWriter) WriteTLSArtifacts(containerService *datamodel.ContainerService, apiVersion, template, parameters, artifactsDir string, certsGenerated bool, parametersOnly bool,
cloudSpecConfig *datamodel.AzureEnvironmentSpecConfig) error {
if len(artifactsDir) == 0 {
artifactsDir = fmt.Sprintf("%s-%s", containerService.Properties.OrchestratorProfile.OrchestratorType, containerService.Properties.GetClusterID())
artifactsDir = path.Join("_output", artifactsDir)
}
f := &helpers.FileSaver{}
// convert back the API object, and write it
var b []byte
var err error
if !parametersOnly {
apiloader := &api.Apiloader{}
b, err = apiloader.SerializeContainerService(containerService, apiVersion)
if err != nil {
return err
}
if e := f.SaveFile(artifactsDir, "apimodel.json", b); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "azuredeploy.json", template); e != nil {
return e
}
}
if e := f.SaveFileString(artifactsDir, "azuredeploy.parameters.json", parameters); e != nil {
return e
}
if !certsGenerated {
return nil
}
properties := containerService.Properties
if properties.OrchestratorProfile.IsKubernetes() {
directory := path.Join(artifactsDir, "kubeconfig")
var locations []string
if containerService.Location != "" {
locations = []string{containerService.Location}
} else {
locations = helpers.GetAzureLocations()
}
for _, location := range locations {
b, gkcerr := GenerateKubeConfig(properties, location, cloudSpecConfig)
if gkcerr != nil {
return gkcerr
}
if e := f.SaveFileString(directory, fmt.Sprintf("kubeconfig.%s.json", location), b); e != nil {
return e
}
}
if e := f.SaveFileString(artifactsDir, "ca.key", properties.CertificateProfile.CaPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "ca.crt", properties.CertificateProfile.CaCertificate); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "apiserver.key", properties.CertificateProfile.APIServerPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "apiserver.crt", properties.CertificateProfile.APIServerCertificate); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "client.key", properties.CertificateProfile.ClientPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "client.crt", properties.CertificateProfile.ClientCertificate); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "kubectlClient.key", properties.CertificateProfile.KubeConfigPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "kubectlClient.crt", properties.CertificateProfile.KubeConfigCertificate); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "etcdserver.key", properties.CertificateProfile.EtcdServerPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "etcdserver.crt", properties.CertificateProfile.EtcdServerCertificate); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "etcdclient.key", properties.CertificateProfile.EtcdClientPrivateKey); e != nil {
return e
}
if e := f.SaveFileString(artifactsDir, "etcdclient.crt", properties.CertificateProfile.EtcdClientCertificate); e != nil {
return e
}
for i := 0; i < properties.MasterProfile.Count; i++ {
if len(properties.CertificateProfile.EtcdPeerPrivateKeys) <= i || len(properties.CertificateProfile.EtcdPeerCertificates) <= i {
return errors.New("missing etcd peer certificate/key pair")
}
k := "etcdpeer" + strconv.Itoa(i) + ".key"
if e := f.SaveFileString(artifactsDir, k, properties.CertificateProfile.EtcdPeerPrivateKeys[i]); e != nil {
return e
}
c := "etcdpeer" + strconv.Itoa(i) + ".crt"
if e := f.SaveFileString(artifactsDir, c, properties.CertificateProfile.EtcdPeerCertificates[i]); e != nil {
return e
}
}
}
return nil
}