Skip to content

Commit

Permalink
Dynamic v3/info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gogolok committed May 12, 2024
1 parent a03dc97 commit 2d6d29d
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 16 deletions.
9 changes: 9 additions & 0 deletions README.helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ Here are all the values that can be set for the chart:
- `host` (_String_): Must be a host string, a host:port pair, or a URL to the base of the apiserver.
- `image` (_String_): Reference to the API container image.
- `include` (_Boolean_): Deploy the API component.
- `infoConfig`: The /v3/info endpoint configuration.
- `build` (_String_): `build` attribute in the /v3/info endpoint
- `custom`: `custom` attribute in the /v3/info endpoint
- `description` (_String_): `description` attribute in the /v3/info endpoint
- `minCLIVersion` (_String_): `minimum` CLI version attribute in the /v3/info endpoint
- `minRecommendedCLIVersion` (_String_): `recommended` CLI version attribute in the /v3/info endpoint
- `name` (_String_): `name` attribute in the /v3/info endpoint
- `supportAddress` (_String_): `support` attribute in the /v3/info endpoint
- `version` (_Integer_): `version` attribute in the /v3/info endpoint
- `lifecycle`: Default lifecycle for apps.
- `stack` (_String_): Stack.
- `type` (_String_): Lifecycle type (only `buildpack` accepted currently).
Expand Down
13 changes: 13 additions & 0 deletions api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type (

ServerURL string

InfoConfig InfoConfig `yaml:"infoConfig"`

RootNamespace string `yaml:"rootNamespace"`
BuilderName string `yaml:"builderName"`
RunnerName string `yaml:"runnerName"`
Expand Down Expand Up @@ -61,6 +63,17 @@ type (
Stack string `yaml:"stack"`
StagingMemoryMB int `yaml:"stagingMemoryMB"`
}

InfoConfig struct {
Build string `yaml:"build"`
Description string `yaml:"description"`
Name string `yaml:"name"`
Version int `yaml:"version"`
MinCLIVersion string `yaml:"minCLIVersion"`
MinRecommendedCLIVersion string `yaml:"minRecommendedCLIVersion"`
Custom map[string]interface{} `yaml:"custom"`
SupportAddress string `yaml:"supportAddress"`
}
)

func LoadFromPath(path string) (*APIConfig, error) {
Expand Down
11 changes: 7 additions & 4 deletions api/handlers/info_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/url"

"code.cloudfoundry.org/korifi/api/config"
"code.cloudfoundry.org/korifi/api/presenter"
"code.cloudfoundry.org/korifi/api/routing"
)
Expand All @@ -13,17 +14,19 @@ const (
)

type InfoV3 struct {
baseURL url.URL
baseURL url.URL
infoConfig config.InfoConfig
}

func NewInfoV3(baseURL url.URL) *InfoV3 {
func NewInfoV3(baseURL url.URL, infoConfig config.InfoConfig) *InfoV3 {
return &InfoV3{
baseURL: baseURL,
baseURL: baseURL,
infoConfig: infoConfig,
}
}

func (h *InfoV3) get(r *http.Request) (*routing.Response, error) {
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForInfoV3(h.baseURL)), nil
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForInfoV3(h.baseURL, h.infoConfig)), nil
}

func (h *InfoV3) UnauthenticatedRoutes() []routing.Route {
Expand Down
11 changes: 9 additions & 2 deletions api/handlers/info_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ package handlers_test
import (
"net/http"

"code.cloudfoundry.org/korifi/api/config"
"code.cloudfoundry.org/korifi/api/handlers"
. "code.cloudfoundry.org/korifi/tests/matchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("InfoV3", func() {
var req *http.Request
var (
req *http.Request
infoConfig config.InfoConfig
)

BeforeEach(func() {
apiHandler := handlers.NewInfoV3(*serverURL)
apiHandler := handlers.NewInfoV3(
*serverURL,
infoConfig,
)
routerBuilder.LoadRoutes(apiHandler)
})

Expand Down
5 changes: 4 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ func main() {
apiHandlers := []routing.Routable{
handlers.NewRootV3(*serverURL),
handlers.NewRoot(*serverURL),
handlers.NewInfoV3(*serverURL),
handlers.NewInfoV3(
*serverURL,
cfg.InfoConfig,
),
handlers.NewResourceMatches(),
handlers.NewApp(
*serverURL,
Expand Down
20 changes: 16 additions & 4 deletions api/presenter/info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package presenter

import "net/url"
import (
"net/url"

"code.cloudfoundry.org/korifi/api/config"
)

type InfoV3Response struct {
Build string `json:"build"`
Expand All @@ -18,15 +22,23 @@ type InfoCLIVersion struct {
Recommended string `json:"recommended"`
}

func ForInfoV3(baseURL url.URL) InfoV3Response {
func ForInfoV3(baseURL url.URL, infoConfig config.InfoConfig) InfoV3Response {
return InfoV3Response{
Custom: make(map[string]interface{}),
Build: infoConfig.Build,
Description: infoConfig.Description,
Name: infoConfig.Name,
Version: infoConfig.Version,
CLIVersion: InfoCLIVersion{
Minimum: infoConfig.MinCLIVersion,
Recommended: infoConfig.MinRecommendedCLIVersion,
},
Custom: emptyMapIfNil(infoConfig.Custom),
Links: map[string]Link{
"self": {
HRef: buildURL(baseURL).appendPath("v3/info").build(),
},
"support": {
HRef: "https://www.cloudfoundry.org/technology/korifi/",
HRef: infoConfig.SupportAddress,
},
},
}
Expand Down
12 changes: 9 additions & 3 deletions api/presenter/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/url"

"code.cloudfoundry.org/korifi/api/config"
"code.cloudfoundry.org/korifi/api/presenter"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -12,19 +13,24 @@ import (

var _ = Describe("Info endpoints", func() {
var (
baseURL *url.URL
output []byte
baseURL *url.URL
infoConfig config.InfoConfig
output []byte
)

BeforeEach(func() {
var err error
baseURL, err = url.Parse("https://api.example.org")
Expect(err).NotTo(HaveOccurred())

infoConfig = config.InfoConfig{
SupportAddress: "https://www.cloudfoundry.org/technology/korifi/",
}
})

Context("/v3/info", func() {
JustBeforeEach(func() {
response := presenter.ForInfoV3(*baseURL)
response := presenter.ForInfoV3(*baseURL, infoConfig)
var err error
output, err = json.Marshal(response)
Expect(err).NotTo(HaveOccurred())
Expand Down
4 changes: 2 additions & 2 deletions api/presenter/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (u buildURL) build() string {
return nativeP.String()
}

func emptyMapIfNil(m map[string]string) map[string]string {
func emptyMapIfNil[V any](m map[string]V) map[string]V {
if m == nil {
return map[string]string{}
return map[string]V{}
}
return m
}
Expand Down
12 changes: 12 additions & 0 deletions helm/korifi/api/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ data:
readTimeout: {{ .Values.api.apiServer.timeouts.read }}
readHeaderTimeout: {{ .Values.api.apiServer.timeouts.readHeader }}
writeTimeout: {{ .Values.api.apiServer.timeouts.write }}
infoConfig:
build: {{ .Values.api.infoConfig.build }}
description: {{ .Values.api.infoConfig.description }}
name: {{ .Values.api.infoConfig.name }}
version: {{ .Values.api.infoConfig.version }}
minCLIVersion: {{ .Values.api.infoConfig.minCLIVersion }}
minRecommendedCLIVersion: {{ .Values.api.infoConfig.minRecommendedCLIVersion }}
{{- with .Values.api.infoConfig.custom }}
custom:
{{- toYaml . | nindent 8 }}
{{- end }}
supportAddress: {{ .Values.api.infoConfig.supportAddress }}
rootNamespace: {{ .Values.rootNamespace }}
builderName: {{ .Values.reconcilers.build }}
runnerName: {{ .Values.reconcilers.run }}
Expand Down
40 changes: 40 additions & 0 deletions helm/korifi/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,46 @@
"description": "Reference to the API container image.",
"type": "string"
},
"infoConfig": {
"type": "object",
"description": "The /v3/info endpoint configuration.",
"properties": {
"build": {
"description": "`build` attribute in the /v3/info endpoint",
"type": "string"
},
"description": {
"description": "`description` attribute in the /v3/info endpoint",
"type": "string"
},
"name": {
"description": "`name` attribute in the /v3/info endpoint",
"type": "string"
},
"version": {
"description": "`version` attribute in the /v3/info endpoint",
"type": "integer"
},
"minCLIVersion": {
"description": "`minimum` CLI version attribute in the /v3/info endpoint",
"type": "string"
},
"minRecommendedCLIVersion": {
"description": "`recommended` CLI version attribute in the /v3/info endpoint",
"type": "string"
},
"custom": {
"description": "`custom` attribute in the /v3/info endpoint",
"type": "object",
"properties": {}
},
"supportAddress": {
"description": "`support` attribute in the /v3/info endpoint",
"type": "string"
}
},
"required": ["build", "description", "name", "version", "minCLIVersion", "minRecommendedCLIVersion", "custom", "supportAddress"]
},
"lifecycle": {
"type": "object",
"description": "Default lifecycle for apps.",
Expand Down
10 changes: 10 additions & 0 deletions helm/korifi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ api:
idle: 900
readHeader: 10

infoConfig:
build: ""
description: ""
name: ""
version: 0
minCLIVersion: ""
minRecommendedCLIVersion: ""
custom: {}
supportAddress: "https://www.cloudfoundry.org/technology/korifi/"

lifecycle:
type: buildpack
stack: cflinuxfs3
Expand Down
1 change: 1 addition & 0 deletions scripts/deploy-on-kind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function deploy_korifi() {
--set=debug="$DEBUG" \
--set=stagingRequirements.buildCacheMB="1024" \
--set=api.apiServer.url="localhost" \
--set=api.infoConfig.build="$VERSION" \
--set=controllers.taskTTL="5s" \
--set=jobTaskRunner.jobTTL="5s" \
--set=containerRepositoryPrefix="$REPOSITORY_PREFIX" \
Expand Down

0 comments on commit 2d6d29d

Please sign in to comment.