Skip to content

Commit 85ded5e

Browse files
authored
feat(api): ChannelName, IsKurl, and Sequence template functions (#3104)
* feat(api): ChannelName, IsKurl, and Sequence template functions * f * f * f
1 parent 0d70c07 commit 85ded5e

File tree

9 files changed

+551
-37
lines changed

9 files changed

+551
-37
lines changed

api/integration/app/install/config_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ func (s *AppInstallTestSuite) TestTemplateAppConfig() {
570570
s.T().Run("Success with default values", func(t *testing.T) {
571571
// Create an install controller with the app config
572572
apiInstance := s.createAPI(t, states.StateNew, &release.ReleaseData{
573-
AppConfig: &appConfigWithTemplates,
573+
AppConfig: &appConfigWithTemplates,
574+
ChannelRelease: &release.ChannelRelease{},
574575
}, nil)
575576

576577
// Create a router and register the API routes
@@ -985,7 +986,8 @@ func (s *AppInstallTestSuite) TestTemplateAppConfig_AdditionalTemplateFunctions(
985986
s.T().Run("Success with default values", func(t *testing.T) {
986987
// Create an install controller with the app config
987988
apiInstance := s.createAPI(t, states.StateNew, &release.ReleaseData{
988-
AppConfig: &appConfigWithTemplates,
989+
AppConfig: &appConfigWithTemplates,
990+
ChannelRelease: &release.ChannelRelease{},
989991
}, nil)
990992

991993
// Create a router and register the API routes

api/integration/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,9 @@ func NewTargetKubernetesAPIWithReleaseData(t *testing.T, mode types.Mode, opts .
108108
func DefaultReleaseData() *release.ReleaseData {
109109
return &release.ReleaseData{
110110
AppConfig: &kotsv1beta1.Config{},
111+
ChannelRelease: &release.ChannelRelease{
112+
ChannelSequence: 1,
113+
ChannelID: "123",
114+
},
111115
}
112116
}

api/pkg/template/execute.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ func (e *Engine) getFuncMap() template.FuncMap {
147147
"LicenseFieldValue": e.licenseFieldValue,
148148
"LicenseDockerCfg": e.licenseDockerCfg,
149149
"VersionLabel": e.versionLabel,
150+
"ChannelName": e.channelName,
151+
"Sequence": e.sequence,
150152

151153
"HTTPProxy": e.httpProxy,
152154
"HTTPSProxy": e.httpsProxy,
@@ -177,6 +179,7 @@ func (e *Engine) getFuncMap() template.FuncMap {
177179
"HumanSize": e.humanSize,
178180
"YamlEscape": e.yamlEscape,
179181
"Distribution": e.distribution,
182+
"IsKurl": func() bool { return false },
180183

181184
// Registry template functions
182185
"HasLocalRegistry": e.hasLocalRegistry,

api/pkg/template/license.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,60 @@ import (
1010
"github.com/replicatedhq/embedded-cluster/pkg/release"
1111
)
1212

13-
func (e *Engine) licenseFieldValue(name string) string {
13+
func (e *Engine) licenseFieldValue(name string) (string, error) {
1414
if e.license == nil {
15-
return ""
15+
return "", fmt.Errorf("license is nil")
1616
}
1717

1818
// Update docs at https://github.com/replicatedhq/kots.io/blob/main/content/reference/template-functions/license-context.md
1919
// when adding new values
2020
switch name {
2121
case "isSnapshotSupported":
22-
return fmt.Sprintf("%t", e.license.Spec.IsSnapshotSupported)
22+
return fmt.Sprintf("%t", e.license.Spec.IsSnapshotSupported), nil
2323
case "IsDisasterRecoverySupported":
24-
return fmt.Sprintf("%t", e.license.Spec.IsDisasterRecoverySupported)
24+
return fmt.Sprintf("%t", e.license.Spec.IsDisasterRecoverySupported), nil
2525
case "isGitOpsSupported":
26-
return fmt.Sprintf("%t", e.license.Spec.IsGitOpsSupported)
26+
return fmt.Sprintf("%t", e.license.Spec.IsGitOpsSupported), nil
2727
case "isSupportBundleUploadSupported":
28-
return fmt.Sprintf("%t", e.license.Spec.IsSupportBundleUploadSupported)
28+
return fmt.Sprintf("%t", e.license.Spec.IsSupportBundleUploadSupported), nil
2929
case "isEmbeddedClusterMultiNodeEnabled":
30-
return fmt.Sprintf("%t", e.license.Spec.IsEmbeddedClusterMultiNodeEnabled)
30+
return fmt.Sprintf("%t", e.license.Spec.IsEmbeddedClusterMultiNodeEnabled), nil
3131
case "isIdentityServiceSupported":
32-
return fmt.Sprintf("%t", e.license.Spec.IsIdentityServiceSupported)
32+
return fmt.Sprintf("%t", e.license.Spec.IsIdentityServiceSupported), nil
3333
case "isGeoaxisSupported":
34-
return fmt.Sprintf("%t", e.license.Spec.IsGeoaxisSupported)
34+
return fmt.Sprintf("%t", e.license.Spec.IsGeoaxisSupported), nil
3535
case "isAirgapSupported":
36-
return fmt.Sprintf("%t", e.license.Spec.IsAirgapSupported)
36+
return fmt.Sprintf("%t", e.license.Spec.IsAirgapSupported), nil
3737
case "licenseType":
38-
return e.license.Spec.LicenseType
38+
return e.license.Spec.LicenseType, nil
3939
case "licenseSequence":
40-
return fmt.Sprintf("%d", e.license.Spec.LicenseSequence)
40+
return fmt.Sprintf("%d", e.license.Spec.LicenseSequence), nil
4141
case "signature":
42-
return string(e.license.Spec.Signature)
42+
return string(e.license.Spec.Signature), nil
4343
case "appSlug":
44-
return e.license.Spec.AppSlug
44+
return e.license.Spec.AppSlug, nil
4545
case "channelID":
46-
return e.license.Spec.ChannelID
46+
return e.license.Spec.ChannelID, nil
4747
case "channelName":
48-
return e.license.Spec.ChannelName
48+
return e.license.Spec.ChannelName, nil
4949
case "isSemverRequired":
50-
return fmt.Sprintf("%t", e.license.Spec.IsSemverRequired)
50+
return fmt.Sprintf("%t", e.license.Spec.IsSemverRequired), nil
5151
case "customerName":
52-
return e.license.Spec.CustomerName
52+
return e.license.Spec.CustomerName, nil
5353
case "licenseID", "licenseId":
54-
return e.license.Spec.LicenseID
54+
return e.license.Spec.LicenseID, nil
5555
case "endpoint":
5656
if e.releaseData == nil {
57-
return ""
57+
return "", fmt.Errorf("release data is nil")
5858
}
5959
ecDomains := utils.GetDomains(e.releaseData)
60-
return netutils.MaybeAddHTTPS(ecDomains.ReplicatedAppDomain)
60+
return netutils.MaybeAddHTTPS(ecDomains.ReplicatedAppDomain), nil
6161
default:
6262
entitlement, ok := e.license.Spec.Entitlements[name]
6363
if ok {
64-
return fmt.Sprintf("%v", entitlement.Value.Value())
64+
return fmt.Sprintf("%v", entitlement.Value.Value()), nil
6565
}
66-
return ""
66+
return "", nil
6767
}
6868
}
6969

@@ -74,6 +74,9 @@ func (e *Engine) licenseDockerCfg() (string, error) {
7474
if e.releaseData == nil {
7575
return "", fmt.Errorf("release data is nil")
7676
}
77+
if e.releaseData.ChannelRelease == nil {
78+
return "", fmt.Errorf("channel release is nil")
79+
}
7780

7881
auth := fmt.Sprintf("%s:%s", e.license.Spec.LicenseID, e.license.Spec.LicenseID)
7982
encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth))
@@ -111,3 +114,25 @@ func getRegistryProxyInfo(releaseData *release.ReleaseData) *registryProxyInfo {
111114
Registry: ecDomains.ReplicatedRegistryDomain,
112115
}
113116
}
117+
118+
func (e *Engine) channelName() (string, error) {
119+
if e.license == nil {
120+
return "", fmt.Errorf("license is nil")
121+
}
122+
if e.releaseData == nil {
123+
return "", fmt.Errorf("release data is nil")
124+
}
125+
if e.releaseData.ChannelRelease == nil {
126+
return "", fmt.Errorf("channel release is nil")
127+
}
128+
129+
for _, channel := range e.license.Spec.Channels {
130+
if channel.ChannelID == e.releaseData.ChannelRelease.ChannelID {
131+
return channel.ChannelName, nil
132+
}
133+
}
134+
if e.license.Spec.ChannelID == e.releaseData.ChannelRelease.ChannelID {
135+
return e.license.Spec.ChannelName, nil
136+
}
137+
return "", fmt.Errorf("channel %s not found in license", e.releaseData.ChannelRelease.ChannelID)
138+
}

0 commit comments

Comments
 (0)