-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Move commonly used modules from keptn/keptn into sub-packages of go-utils #483
Changes from all commits
090323f
16a3f81
b9c7a75
dc3f6c8
0a59f74
783bc52
c6540c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/benbjohnson/clock" | ||
"github.com/keptn/go-utils/pkg/api/models" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// InternalAPISet is an implementation of APISet | ||
// which can be used from within the Keptn control plane | ||
type InternalAPISet struct { | ||
apimap InClusterAPIMappings | ||
httpClient *http.Client | ||
apiHandler *InternalAPIHandler | ||
authHandler *AuthHandler | ||
eventHandler *EventHandler | ||
logHandler *LogHandler | ||
projectHandler *ProjectHandler | ||
resourceHandler *ResourceHandler | ||
secretHandler *SecretHandler | ||
sequenceControlHandler *SequenceControlHandler | ||
serviceHandler *ServiceHandler | ||
stageHandler *StageHandler | ||
uniformHandler *UniformHandler | ||
shipyardControlHandler *ShipyardControllerHandler | ||
} | ||
|
||
// InternalService is used to enumerate internal Keptn services | ||
type InternalService int | ||
|
||
const ( | ||
ConfigurationService InternalService = iota | ||
ShipyardController | ||
ApiService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
SecretService | ||
MongoDBDatastore | ||
) | ||
|
||
// InClusterAPIMappings maps a keptn service name to its reachable domain name | ||
type InClusterAPIMappings map[InternalService]string | ||
|
||
// DefaultInClusterAPIMappings gives you the default InClusterAPIMappings | ||
var DefaultInClusterAPIMappings = InClusterAPIMappings{ | ||
ConfigurationService: "configuration-service:8080", | ||
ShipyardController: "shipyard-controller:8080", | ||
ApiService: "api-service:8080", | ||
SecretService: "secret-service:8080", | ||
MongoDBDatastore: "mongodb-datastore:8080", | ||
} | ||
|
||
// NewInternal creates a new InternalAPISet usable for calling keptn services from within the control plane | ||
func NewInternal(client *http.Client, apiMappings ...InClusterAPIMappings) (*InternalAPISet, error) { | ||
var apimap InClusterAPIMappings | ||
if len(apiMappings) > 0 { | ||
apimap = apiMappings[0] | ||
} else { | ||
apimap = DefaultInClusterAPIMappings | ||
} | ||
|
||
if client == nil { | ||
client = &http.Client{} | ||
} | ||
|
||
as := &InternalAPISet{} | ||
as.httpClient = client | ||
|
||
as.apiHandler = &InternalAPIHandler{ | ||
shipyardControllerApiHandler: &APIHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
}, | ||
} | ||
|
||
as.authHandler = &AuthHandler{ | ||
BaseURL: apimap[ApiService], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.logHandler = &LogHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: getClientTransport(as.httpClient.Transport)}, | ||
Scheme: "http", | ||
LogCache: []models.LogEntry{}, | ||
TheClock: clock.New(), | ||
SyncInterval: 1 * time.Minute, | ||
} | ||
|
||
as.eventHandler = &EventHandler{ | ||
BaseURL: apimap[MongoDBDatastore], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
|
||
as.projectHandler = &ProjectHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
|
||
as.resourceHandler = &ResourceHandler{ | ||
BaseURL: apimap[ConfigurationService], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.secretHandler = &SecretHandler{ | ||
BaseURL: apimap[SecretService], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.sequenceControlHandler = &SequenceControlHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.serviceHandler = &ServiceHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.shipyardControlHandler = &ShipyardControllerHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: wrapOtelTransport(getClientTransport(as.httpClient.Transport))}, | ||
Scheme: "http", | ||
} | ||
as.stageHandler = &StageHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: otelhttp.NewTransport(as.httpClient.Transport)}, | ||
Scheme: "http", | ||
} | ||
as.uniformHandler = &UniformHandler{ | ||
BaseURL: apimap[ShipyardController], | ||
HTTPClient: &http.Client{Transport: getClientTransport(as.httpClient.Transport)}, | ||
Scheme: "http", | ||
} | ||
return as, nil | ||
} | ||
|
||
// APIV1 retrieves the APIHandler | ||
func (c *InternalAPISet) APIV1() APIV1Interface { | ||
return c.apiHandler | ||
} | ||
|
||
// AuthV1 retrieves the AuthHandler | ||
func (c *InternalAPISet) AuthV1() AuthV1Interface { | ||
return c.authHandler | ||
} | ||
|
||
// EventsV1 retrieves the EventHandler | ||
func (c *InternalAPISet) EventsV1() EventsV1Interface { | ||
return c.eventHandler | ||
} | ||
|
||
// LogsV1 retrieves the LogHandler | ||
func (c *InternalAPISet) LogsV1() LogsV1Interface { | ||
return c.logHandler | ||
} | ||
|
||
// ProjectsV1 retrieves the ProjectHandler | ||
func (c *InternalAPISet) ProjectsV1() ProjectsV1Interface { | ||
return c.projectHandler | ||
} | ||
|
||
// ResourcesV1 retrieves the ResourceHandler | ||
func (c *InternalAPISet) ResourcesV1() ResourcesV1Interface { | ||
return c.resourceHandler | ||
} | ||
|
||
// SecretsV1 retrieves the SecretHandler | ||
func (c *InternalAPISet) SecretsV1() SecretsV1Interface { | ||
return c.secretHandler | ||
} | ||
|
||
// SequencesV1 retrieves the SequenceControlHandler | ||
func (c *InternalAPISet) SequencesV1() SequencesV1Interface { | ||
return c.sequenceControlHandler | ||
} | ||
|
||
// ServicesV1 retrieves the ServiceHandler | ||
func (c *InternalAPISet) ServicesV1() ServicesV1Interface { | ||
return c.serviceHandler | ||
} | ||
|
||
// StagesV1 retrieves the StageHandler | ||
func (c *InternalAPISet) StagesV1() StagesV1Interface { | ||
return c.stageHandler | ||
} | ||
|
||
// UniformV1 retrieves the UniformHandler | ||
func (c *InternalAPISet) UniformV1() UniformV1Interface { | ||
return c.uniformHandler | ||
} | ||
|
||
// ShipyardControlV1 retrieves the ShipyardControllerHandler | ||
func (c *InternalAPISet) ShipyardControlV1() ShipyardControlV1Interface { | ||
return c.shipyardControlHandler | ||
} | ||
|
||
// InternalAPIHandler is used instead of APIHandler from go-utils because we cannot support | ||
// (unauthenticated) internal calls to the api-service at the moment. So this implementation | ||
// will panic as soon as a client wants to call these methods | ||
type InternalAPIHandler struct { | ||
shipyardControllerApiHandler *APIHandler | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
} | ||
|
||
func (i *InternalAPIHandler) SendEvent(event models.KeptnContextExtendedCE) (*models.EventContext, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
panic("SendEvent() is not not supported for internal usage") | ||
} | ||
|
||
func (i *InternalAPIHandler) TriggerEvaluation(project string, stage string, service string, evaluation models.Evaluation) (*models.EventContext, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.TriggerEvaluation(project, stage, service, evaluation) | ||
} | ||
|
||
func (i *InternalAPIHandler) CreateProject(project models.CreateProject) (string, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.CreateProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) UpdateProject(project models.CreateProject) (string, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.UpdateProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) DeleteProject(project models.Project) (*models.DeleteProjectResponse, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.DeleteProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) CreateService(project string, service models.CreateService) (string, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.CreateService(project, service) | ||
} | ||
|
||
func (i *InternalAPIHandler) DeleteService(project string, service string) (*models.DeleteServiceResponse, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
return i.shipyardControllerApiHandler.DeleteService(project, service) | ||
} | ||
|
||
func (i *InternalAPIHandler) GetMetadata() (*models.Metadata, *models.Error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [golint] reported by reviewdog 🐶 |
||
panic("GetMetadata() is not not supported for internal usage") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestApiSetInternalMappings(t *testing.T) { | ||
t.Run("TestInternalAPISet - Default API Mappings", func(t *testing.T) { | ||
internal, err := NewInternal(nil) | ||
require.Nil(t, err) | ||
require.NotNil(t, internal) | ||
assert.Equal(t, DefaultInClusterAPIMappings[MongoDBDatastore], internal.EventsV1().(*EventHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ApiService], internal.AuthV1().(*AuthHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.APIV1().(*InternalAPIHandler).shipyardControllerApiHandler.BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.ShipyardControlV1().(*ShipyardControllerHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.UniformV1().(*UniformHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.LogsV1().(*LogHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.SequencesV1().(*SequenceControlHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.StagesV1().(*StageHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[SecretService], internal.SecretsV1().(*SecretHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ConfigurationService], internal.ResourcesV1().(*ResourceHandler).BaseURL) | ||
assert.Equal(t, DefaultInClusterAPIMappings[ShipyardController], internal.ProjectsV1().(*ProjectHandler).BaseURL) | ||
}) | ||
|
||
t.Run("TestInternalAPISet - Override Mappings", func(t *testing.T) { | ||
overrideMappings := InClusterAPIMappings{ | ||
ConfigurationService: "special-configuration-service:8080", | ||
ShipyardController: "special-shipyard-controller:8080", | ||
ApiService: "speclial-api-service:8080", | ||
SecretService: "special-secret-service:8080", | ||
MongoDBDatastore: "special-monogodb-datastore:8080", | ||
} | ||
internal, err := NewInternal(nil, overrideMappings) | ||
require.Nil(t, err) | ||
require.NotNil(t, internal) | ||
assert.Equal(t, overrideMappings[MongoDBDatastore], internal.EventsV1().(*EventHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ApiService], internal.AuthV1().(*AuthHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.APIV1().(*InternalAPIHandler).shipyardControllerApiHandler.BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.ShipyardControlV1().(*ShipyardControllerHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.UniformV1().(*UniformHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.LogsV1().(*LogHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.SequencesV1().(*SequenceControlHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.StagesV1().(*StageHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[SecretService], internal.SecretsV1().(*SecretHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ConfigurationService], internal.ResourcesV1().(*ResourceHandler).BaseURL) | ||
assert.Equal(t, overrideMappings[ShipyardController], internal.ProjectsV1().(*ProjectHandler).BaseURL) | ||
}) | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[golint] reported by reviewdog 🐶
exported const ConfigurationService should have comment (or a comment on this block) or be unexported