Skip to content

Commit af6a093

Browse files
authored
Add information about availability of packages in serverless projects (#1516)
Extend `elastic-package status` to optionally include the packages available on each serverless project type. To include this information, the `--info serverless.project_types` flag can be used. It requests the current serverless Kibana configuration from the Kibana repository. This information is temporarily cached in the data directory to avoid making too many requests.
1 parent af194a4 commit af6a093

File tree

8 files changed

+366
-16
lines changed

8 files changed

+366
-16
lines changed

cmd/status.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"net/http"
1112
"os"
13+
"slices"
1214
"strings"
1315

1416
"github.com/Masterminds/semver/v3"
@@ -32,8 +34,9 @@ package directory and reports its status.`
3234

3335
const (
3436
kibanaVersionParameter = "kibana.version"
35-
categoriesParamter = "categories"
37+
categoriesParameter = "categories"
3638
elasticsearchSubscriptionParameter = "elastic.subscription"
39+
serverlessProjectTypesParameter = "serverless.project_types"
3740
)
3841

3942
var (
@@ -43,8 +46,9 @@ var (
4346

4447
availableExtraInfoParameters = []string{
4548
kibanaVersionParameter,
46-
categoriesParamter,
49+
categoriesParameter,
4750
elasticsearchSubscriptionParameter,
51+
serverlessProjectTypesParameter,
4852
}
4953
)
5054

@@ -100,6 +104,17 @@ func statusCommandAction(cmd *cobra.Command, args []string) error {
100104
if err != nil {
101105
return err
102106
}
107+
108+
if slices.Contains(extraParameters, serverlessProjectTypesParameter) {
109+
if packageName == "" && packageStatus.Local != nil {
110+
packageName = packageStatus.Local.Name
111+
}
112+
packageStatus.Serverless, err = getServerlessManifests(packageName, options)
113+
if err != nil {
114+
return err
115+
}
116+
}
117+
103118
return print(packageStatus, os.Stdout, extraParameters)
104119
}
105120

@@ -132,6 +147,32 @@ func getPackageStatus(packageName string, options registry.SearchOptions) (*stat
132147
return status.LocalPackage(packageRootPath, options)
133148
}
134149

150+
func getServerlessManifests(packageName string, options registry.SearchOptions) ([]status.ServerlessManifests, error) {
151+
if packageName == "" {
152+
return nil, nil
153+
}
154+
var serverless []status.ServerlessManifests
155+
projectTypes := status.GetServerlessProjectTypes(http.DefaultClient)
156+
for _, projectType := range projectTypes {
157+
if slices.Contains(projectType.ExcludePackages, packageName) {
158+
continue
159+
}
160+
options := options
161+
options.Capabilities = projectType.Capabilities
162+
options.SpecMax = projectType.SpecMax
163+
options.SpecMin = projectType.SpecMin
164+
manifests, err := registry.Production.Revisions(packageName, options)
165+
if err != nil {
166+
return nil, fmt.Errorf("failed to get packages available for serverless projects of type %s: %w", projectType.Name, err)
167+
}
168+
serverless = append(serverless, status.ServerlessManifests{
169+
Name: projectType.Name,
170+
Manifests: manifests,
171+
})
172+
}
173+
return serverless, nil
174+
}
175+
135176
// print formats and prints package information into a table
136177
func print(p *status.PackageStatus, w io.Writer, extraParameters []string) error {
137178
bold.Fprint(w, "Package: ")
@@ -180,9 +221,15 @@ func renderPendingChanges(p *status.PackageStatus, w io.Writer) {
180221
func renderPackageVersions(p *status.PackageStatus, w io.Writer, extraParameters []string) {
181222
var environmentTable [][]string
182223
if p.Local != nil {
183-
environmentTable = append(environmentTable, formatManifest("Local", *p.Local, nil, extraParameters))
224+
environmentTable = append(environmentTable, formatManifest("Local", "-", *p.Local, nil, extraParameters))
225+
}
226+
data := formatManifests("Production", "-", p.Production, extraParameters)
227+
environmentTable = append(environmentTable, data)
228+
229+
for _, projectType := range p.Serverless {
230+
data := formatManifests("Production", projectType.Name, projectType.Manifests, extraParameters)
231+
environmentTable = append(environmentTable, data)
184232
}
185-
environmentTable = append(environmentTable, formatManifests("Production", p.Production, extraParameters))
186233

187234
bold.Fprintln(w, "Package Versions:")
188235
table := tablewriter.NewWriter(w)
@@ -230,7 +277,7 @@ func formatChangelogEntry(change changelog.Entry) []string {
230277
}
231278

232279
// formatManifests returns a row of data ffor a set of versioned packaged manifests
233-
func formatManifests(environment string, manifests []packages.PackageManifest, extraParameters []string) []string {
280+
func formatManifests(environment string, serverless string, manifests []packages.PackageManifest, extraParameters []string) []string {
234281
if len(manifests) == 0 {
235282
return []string{environment, "-", "-", "-", "-"}
236283
}
@@ -240,11 +287,11 @@ func formatManifests(environment string, manifests []packages.PackageManifest, e
240287
extraVersions = append(extraVersions, m.Version)
241288
}
242289
}
243-
return formatManifest(environment, manifests[len(manifests)-1], extraVersions, extraParameters)
290+
return formatManifest(environment, serverless, manifests[len(manifests)-1], extraVersions, extraParameters)
244291
}
245292

246293
// formatManifest returns a row of data for a given package manifest
247-
func formatManifest(environment string, manifest packages.PackageManifest, extraVersions []string, extraParameters []string) []string {
294+
func formatManifest(environment string, serverless string, manifest packages.PackageManifest, extraVersions []string, extraParameters []string) []string {
248295
version := manifest.Version
249296
if len(extraVersions) > 0 {
250297
version = fmt.Sprintf("%s (%s)", version, strings.Join(extraVersions, ", "))
@@ -256,10 +303,12 @@ func formatManifest(environment string, manifest packages.PackageManifest, extra
256303
switch param {
257304
case kibanaVersionParameter:
258305
data = append(data, manifest.Conditions.Kibana.Version)
259-
case categoriesParamter:
306+
case categoriesParameter:
260307
data = append(data, strings.Join(manifest.Categories, ","))
261308
case elasticsearchSubscriptionParameter:
262309
data = append(data, manifest.Conditions.Elastic.Subscription)
310+
case serverlessProjectTypesParameter:
311+
data = append(data, serverless)
263312
}
264313
}
265314
return data

cmd/status_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ func TestStatusFormatAndPrint(t *testing.T) {
149149
},
150150
expected: "./testdata/status-extra-parameters",
151151
},
152+
{
153+
title: "extra serverless parameter",
154+
pkgStatus: &status.PackageStatus{
155+
Name: "foo",
156+
Local: &localPackage,
157+
Production: []packages.PackageManifest{
158+
fooPackage("1.0.0", "^8.8.0"),
159+
},
160+
Serverless: []status.ServerlessManifests{
161+
{
162+
Name: "observability",
163+
Manifests: []packages.PackageManifest{
164+
fooPackage("1.0.0", "^8.8.0"),
165+
},
166+
},
167+
{
168+
Name: "security",
169+
Manifests: []packages.PackageManifest{
170+
fooPackage("1.0.0", "^8.8.0"),
171+
},
172+
},
173+
},
174+
},
175+
extraParameters: []string{
176+
"serverless.project_types",
177+
},
178+
expected: "./testdata/status-serverless-projects",
179+
},
152180
}
153181

154182
for _, c := range cases {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Package: foo
2+
Owner: team
3+
Package Versions:
4+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+
5+
| ENVIRONMENT | VERSION | RELEASE | TITLE | DESCRIPTION | SERVERLESS PROJECT TYPES |
6+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+
7+
| Local | 2.0.0-rc1 | Release Candidate | Foo | Foo integration | - |
8+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+
9+
| Production | 1.0.0 | GA | Foo | Foo integration | - |
10+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+
11+
| Production | 1.0.0 | GA | Foo | Foo integration | observability |
12+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+
13+
| Production | 1.0.0 | GA | Foo | Foo integration | security |
14+
+-------------+-----------+-------------------+-------+-----------------+--------------------------+

internal/configuration/locations/locations.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ const (
2222
temporaryDir = "tmp"
2323
deployerDir = "deployer"
2424

25-
fieldsCachedDir = "cache/fields"
25+
cacheDir = "cache"
26+
FieldsCacheName = "fields"
27+
KibanaConfigCacheName = "kibana_config"
2628
)
2729

2830
var (
@@ -95,9 +97,9 @@ func (loc LocationManager) ServiceOutputDir() string {
9597
return filepath.Join(loc.stackPath, serviceOutputDir)
9698
}
9799

98-
// FieldsCacheDir returns the directory with cached fields
99-
func (loc LocationManager) FieldsCacheDir() string {
100-
return filepath.Join(loc.stackPath, fieldsCachedDir)
100+
// CacheDir returns the directory with cached fields
101+
func (loc LocationManager) CacheDir(name string) string {
102+
return filepath.Join(loc.stackPath, cacheDir, name)
101103
}
102104

103105
// configurationDir returns the configuration directory location

internal/fields/dependency_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func readECSFieldsSchemaFile(dep buildmanifest.ECSDependency) ([]byte, error) {
8585
if err != nil {
8686
return nil, fmt.Errorf("error fetching profile path: %w", err)
8787
}
88-
cachedSchemaPath := filepath.Join(loc.FieldsCacheDir(), ecsSchemaName, gitReference, ecsSchemaFile)
88+
cachedSchemaPath := filepath.Join(loc.CacheDir(locations.FieldsCacheName), ecsSchemaName, gitReference, ecsSchemaFile)
8989
content, err := os.ReadFile(cachedSchemaPath)
9090
if errors.Is(err, os.ErrNotExist) {
9191
logger.Debugf("Pulling ECS dependency using reference: %s", dep.Reference)

0 commit comments

Comments
 (0)