This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathcnab.go
More file actions
206 lines (196 loc) · 5.14 KB
/
cnab.go
File metadata and controls
206 lines (196 loc) · 5.14 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package packager
import (
"encoding/json"
"github.com/deislabs/cnab-go/bundle"
"github.com/deislabs/cnab-go/bundle/definition"
"github.com/docker/app/internal"
"github.com/docker/app/internal/compose"
"github.com/docker/app/types"
"github.com/sirupsen/logrus"
)
const (
// CNABVersion1_0_0 is the CNAB Schema version 1.0.0
CNABVersion1_0_0 = "v1.0.0-WD"
)
// ToCNAB creates a CNAB bundle from an app package
func ToCNAB(app *types.App, invocationImageName string) (*bundle.Bundle, error) {
mapping := ExtractCNABParameterMapping(app.Parameters())
flatParameters := app.Parameters().Flatten()
definitions := definition.Definitions{
internal.ParameterOrchestratorName: {
Type: "string",
Enum: []interface{}{
"",
"swarm",
"kubernetes",
},
Default: "",
Title: "Orchestrator",
Description: "Orchestrator on which to deploy",
},
internal.ParameterKubernetesNamespaceName: {
Type: "string",
Default: "",
Title: "Namespace",
Description: "Namespace in which to deploy",
},
internal.ParameterRenderFormatName: {
Type: "string",
Enum: []interface{}{
"yaml",
"json",
},
Default: "yaml",
Title: "Render format",
Description: "Output format for the render command",
},
internal.ParameterShareRegistryCredsName: {
Type: "boolean",
Default: false,
Title: "Share registry credentials",
Description: "Share registry credentials with the invocation image",
},
}
parameters := map[string]bundle.Parameter{
internal.ParameterOrchestratorName: {
Destination: &bundle.Location{
EnvironmentVariable: internal.DockerStackOrchestratorEnvVar,
},
ApplyTo: []string{
"install",
"upgrade",
"uninstall",
internal.ActionStatusName,
},
Definition: internal.ParameterOrchestratorName,
},
internal.ParameterKubernetesNamespaceName: {
Destination: &bundle.Location{
EnvironmentVariable: internal.DockerKubernetesNamespaceEnvVar,
},
ApplyTo: []string{
"install",
"upgrade",
"uninstall",
internal.ActionStatusName,
},
Definition: internal.ParameterKubernetesNamespaceName,
},
internal.ParameterRenderFormatName: {
Destination: &bundle.Location{
EnvironmentVariable: internal.DockerRenderFormatEnvVar,
},
ApplyTo: []string{
internal.ActionRenderName,
},
Definition: internal.ParameterRenderFormatName,
},
internal.ParameterShareRegistryCredsName: {
Destination: &bundle.Location{
EnvironmentVariable: "DOCKER_SHARE_REGISTRY_CREDS",
},
Definition: internal.ParameterShareRegistryCredsName,
},
}
for name, envVar := range mapping.ParameterToCNABEnv {
definitions[name] = &definition.Schema{
Type: "string",
Default: flatParameters[name],
}
parameters[name] = bundle.Parameter{
Destination: &bundle.Location{
EnvironmentVariable: envVar,
},
Definition: name,
}
}
for name, secret := range app.Secrets() {
writeOnly := new(bool)
*writeOnly = true
definitions[internal.SecretsParameterPrefix+name] = &definition.Schema{
Type: "string",
WriteOnly: writeOnly,
}
parameters[internal.SecretsParameterPrefix+name] = bundle.Parameter{
Destination: &bundle.Location{
Path: secret.NormalizeFilename(),
},
Definition: internal.SecretsParameterPrefix + name,
}
}
var maintainers []bundle.Maintainer
for _, m := range app.Metadata().Maintainers {
maintainers = append(maintainers, bundle.Maintainer{
Email: m.Email,
Name: m.Name,
})
}
bundleImages, err := extractBundleImages(app.Composes())
if err != nil {
return nil, err
}
bndl := &bundle.Bundle{
SchemaVersion: CNABVersion1_0_0,
Credentials: map[string]bundle.Credential{
internal.CredentialDockerContextName: {
Location: bundle.Location{
Path: internal.CredentialDockerContextPath,
},
},
internal.CredentialRegistryName: {
Location: bundle.Location{
Path: internal.CredentialRegistryPath,
},
},
},
Description: app.Metadata().Description,
InvocationImages: []bundle.InvocationImage{
{
BaseImage: bundle.BaseImage{
Image: invocationImageName,
ImageType: "docker",
},
},
},
Maintainers: maintainers,
Name: app.Metadata().Name,
Version: app.Metadata().Version,
Parameters: parameters,
Definitions: definitions,
Actions: map[string]bundle.Action{
internal.ActionInspectName: {
Modifies: false,
Stateless: true,
},
internal.ActionRenderName: {
Modifies: false,
Stateless: true,
},
internal.ActionStatusName: {
Modifies: false,
},
},
Images: bundleImages,
}
if js, err := json.Marshal(bndl); err == nil {
logrus.Debugf("App converted to CNAB %q", string(js))
}
return bndl, nil
}
func extractBundleImages(composeFiles [][]byte) (map[string]bundle.Image, error) {
_, images, err := compose.Load(composeFiles)
if err != nil {
return nil, err
}
bundleImages := map[string]bundle.Image{}
for serviceName, imageName := range images {
bundleImages[serviceName] = bundle.Image{
Description: imageName,
BaseImage: bundle.BaseImage{
Image: imageName,
ImageType: "docker",
},
}
}
return bundleImages, nil
}