Skip to content

Commit

Permalink
Add edgeconnect provisioner mode (#2298)
Browse files Browse the repository at this point in the history
* added edgeconnect provisioner mode
  • Loading branch information
aorcholski authored Nov 21, 2023
1 parent 005a67c commit 8fa3f09
Show file tree
Hide file tree
Showing 14 changed files with 1,058 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ packages:
github.com/Dynatrace/dynatrace-operator/pkg/clients/dynatrace:
interfaces:
Client:
github.com/Dynatrace/dynatrace-operator/pkg/clients/edgeconnect:
interfaces:
Client:
github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/version:
interfaces:
StatusUpdater:
Expand Down
11 changes: 11 additions & 0 deletions config/crd/bases/dynatrace.com_edgeconnects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ spec:
- name
type: object
type: array
hostPatterns:
description: Host patterns to be set in the tenant, only considered
when provisioning is enabled.
items:
type: string
type: array
hostRestrictions:
description: Restrict outgoing HTTP requests to your internal resources
to specified hosts
Expand Down Expand Up @@ -210,6 +216,11 @@ spec:
endpoint:
description: Token endpoint URL of Dynatrace SSO
type: string
provisioner:
description: Determines if the operator will create the EdgeConnect
and light OAuth client on the cluster using the credentials
provided. Requires more scopes than default behavior.
type: boolean
resource:
description: URN identifying your account. You get the URN when
creating the OAuth client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,12 @@ spec:
- name
type: object
type: array
hostPatterns:
description: Host patterns to be set in the tenant, only considered
when provisioning is enabled.
items:
type: string
type: array
hostRestrictions:
description: Restrict outgoing HTTP requests to your internal resources
to specified hosts
Expand Down Expand Up @@ -3888,6 +3894,11 @@ spec:
endpoint:
description: Token endpoint URL of Dynatrace SSO
type: string
provisioner:
description: Determines if the operator will create the EdgeConnect
and light OAuth client on the cluster using the credentials
provided. Requires more scopes than default behavior.
type: boolean
resource:
description: URN identifying your account. You get the URN when
creating the OAuth client
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/v1alpha1/edgeconnect/edgeconnect_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type EdgeConnectSpec struct { //nolint:revive

// Sets topology spread constraints for the EdgeConnect pods
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`

// Host patterns to be set in the tenant, only considered when provisioning is enabled.
// +kubebuilder:validation:Optional
HostPatterns []string `json:"hostPatterns,omitempty"`
}

type OAuthSpec struct {
Expand All @@ -73,6 +77,9 @@ type OAuthSpec struct {
// URN identifying your account. You get the URN when creating the OAuth client
// +kubebuilder:validation:Required
Resource string `json:"resource"`
// Determines if the operator will create the EdgeConnect and light OAuth client on the cluster using the credentials provided. Requires more scopes than default behavior.
// +kubebuilder:validation:Optional
Provisioner bool `json:"provisioner"`
}

type ImageRefSpec struct {
Expand Down
44 changes: 41 additions & 3 deletions pkg/clients/edgeconnect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"golang.org/x/oauth2/clientcredentials"
)

const (
contentTypeJSON = "application/json"
)

type client struct {
ctx context.Context
baseURL string
Expand Down Expand Up @@ -193,6 +197,8 @@ func (c *client) UpdateEdgeConnect(edgeConnectId, name string, hostPatterns []st
return err
}

req.Header.Set("Content-Type", contentTypeJSON)

resp, err := c.httpClient.Do(req)
defer utils.CloseBodyAfterRequest(resp)

Expand All @@ -214,7 +220,6 @@ func (c *client) UpdateEdgeConnect(edgeConnectId, name string, hostPatterns []st

// DeleteEdgeConnect deletes edge connect using DELETE method for give edgeConnectId
func (c *client) DeleteEdgeConnect(edgeConnectId string) error {
log.Info("started removing edge connect %s", edgeConnectId)
url := c.getEdgeConnectUrl(edgeConnectId)

req, err := http.NewRequest(http.MethodDelete, url, nil)
Expand All @@ -237,7 +242,6 @@ func (c *client) DeleteEdgeConnect(edgeConnectId string) error {
}
return errors.Errorf("edgeconnect server error %d: %s", errorResponse.ErrorMessage.Code, errorResponse.ErrorMessage.Message)
}
log.Info("finished removing edge connect %s", edgeConnectId)
return nil
}

Expand All @@ -256,7 +260,7 @@ func (c *client) CreateEdgeConnect(name string, hostPatterns []string, oauthClie
return CreateResponse{}, err
}

resp, err := c.httpClient.Post(url, http.MethodPost, payloadBuf)
resp, err := c.httpClient.Post(url, contentTypeJSON, payloadBuf)
defer utils.CloseBodyAfterRequest(resp)

if err != nil {
Expand All @@ -276,3 +280,37 @@ func (c *client) CreateEdgeConnect(name string, hostPatterns []string, oauthClie

return response, nil
}

// GetEdgeConnects returns list of edge connects
func (c *client) GetEdgeConnects(name string) (ListResponse, error) {
ecUrl := c.getEdgeConnectsUrl()

req, err := http.NewRequest("GET", ecUrl, nil)
if err != nil {
return ListResponse{}, err
}
req.URL.RawQuery = url.Values{
"add-fields": {"name"},
"filter": {fmt.Sprintf("name='%s'", name)},
}.Encode()

resp, err := c.httpClient.Do(req)
defer utils.CloseBodyAfterRequest(resp)

if err != nil {
return ListResponse{}, err
}

responseData, err := c.getServerResponseData(resp)
if err != nil {
return ListResponse{}, err
}

response := ListResponse{}
err = json.Unmarshal(responseData, &response)
if err != nil {
return ListResponse{}, err
}

return response, nil
}
9 changes: 0 additions & 9 deletions pkg/clients/edgeconnect/config.go

This file was deleted.

18 changes: 13 additions & 5 deletions pkg/clients/edgeconnect/edgeconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ type GetResponse struct {
Metadata Metadata `json:"metadata"`
}

type ListResponse struct {
EdgeConnects []GetResponse `json:"edgeConnects"`
TotalCount int `json:"totalCount"`
}

type CreateResponse struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
HostPatterns []string `json:"hostPatterns"`
OauthClientId string `json:"oauthClientId"`
ModificationInfo ModificationInfo `json:"modificationInfo"`
ID string `json:"id,omitempty"`
Name string `json:"name"`
HostPatterns []string `json:"hostPatterns"`
OauthClientId string `json:"oauthClientId"`
OauthClientSecret string `json:"oauthClientSecret"`
OauthClientResource string `json:"oauthClientResource"`
ModificationInfo ModificationInfo `json:"modificationInfo"`
Metadata Metadata `json:"metadata"`
}

type Request struct {
Expand Down
3 changes: 3 additions & 0 deletions pkg/clients/edgeconnect/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ type Client interface {

// DeleteEdgeConnect deletes edge connect
DeleteEdgeConnect(edgeConnectId string) error

// GetEdgeConnects returns list of edge connects
GetEdgeConnects(name string) (ListResponse, error)
}
2 changes: 2 additions & 0 deletions pkg/controllers/edgeconnect/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
PathEdgeConnectOauthClientID = "oauth/client_id"
KeyEdgeConnectOauthClientSecret = "oauth-client-secret"
PathEdgeConnectOauthClientSecret = "oauth/client_secret"
KeyEdgeConnectOauthResource = "oauth-client-resource"
KeyEdgeConnectId = "id"

AnnotationEdgeConnectContainerAppArmor = "container.apparmor.security.beta.kubernetes.io/" + EdgeConnectContainerName
)
Loading

0 comments on commit 8fa3f09

Please sign in to comment.