This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Move commonly used modules from keptn/keptn into sub-packages o…
…f go-utils (#483) * feat: Move commonly used modules from keptn/keptn into sub-packages of go-utils Signed-off-by: Florian Bacher <[email protected]> * feat: Move commonly used modules from keptn/keptn into sub-packages of go-utils Signed-off-by: Florian Bacher <[email protected]> * merge master Signed-off-by: Florian Bacher <[email protected]> * move internal api client utils to api package Signed-off-by: Florian Bacher <[email protected]> * try to fix sync issue Signed-off-by: Florian Bacher <[email protected]> * remove -race flag from test Signed-off-by: Florian Bacher <[email protected]>
- Loading branch information
Showing
50 changed files
with
6,493 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
} | ||
|
||
func (i *InternalAPIHandler) SendEvent(event models.KeptnContextExtendedCE) (*models.EventContext, *models.Error) { | ||
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) { | ||
return i.shipyardControllerApiHandler.TriggerEvaluation(project, stage, service, evaluation) | ||
} | ||
|
||
func (i *InternalAPIHandler) CreateProject(project models.CreateProject) (string, *models.Error) { | ||
return i.shipyardControllerApiHandler.CreateProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) UpdateProject(project models.CreateProject) (string, *models.Error) { | ||
return i.shipyardControllerApiHandler.UpdateProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) DeleteProject(project models.Project) (*models.DeleteProjectResponse, *models.Error) { | ||
return i.shipyardControllerApiHandler.DeleteProject(project) | ||
} | ||
|
||
func (i *InternalAPIHandler) CreateService(project string, service models.CreateService) (string, *models.Error) { | ||
return i.shipyardControllerApiHandler.CreateService(project, service) | ||
} | ||
|
||
func (i *InternalAPIHandler) DeleteService(project string, service string) (*models.DeleteServiceResponse, *models.Error) { | ||
return i.shipyardControllerApiHandler.DeleteService(project, service) | ||
} | ||
|
||
func (i *InternalAPIHandler) GetMetadata() (*models.Metadata, *models.Error) { | ||
panic("GetMetadata() is not not supported for internal usage") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|
||
} |
Oops, something went wrong.