Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit 97c79c9

Browse files
committed
deletes pipelines on space removal
pipelines doesnt get deleted while removing a space which leaves dangling pipelines in OpenShift. This patch deletes the pipelines while removing a space Patch adds - REST API to delete pipeline by space label - Test cases for delete pipeline(buildconfig) function - Delete BuildConfig API in OpenShift client - Test cases for delete BuildConfig API - Refactors kubernetes_client to get client objects - Fixes delete OpenShift resources tests Fixes -openshiftio/openshift.io#3802
1 parent c81a4d2 commit 97c79c9

15 files changed

+970
-149
lines changed

controller/deployments.go

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"io"
7-
"net/url"
8-
"os"
97
"time"
108

119
"github.com/fabric8-services/fabric8-wit/app"
@@ -17,10 +15,10 @@ import (
1715

1816
"github.com/goadesign/goa"
1917
errs "github.com/pkg/errors"
20-
uuid "github.com/satori/go.uuid"
18+
"github.com/satori/go.uuid"
2119
"golang.org/x/net/websocket"
2220
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23-
v1 "k8s.io/client-go/pkg/api/v1"
21+
"k8s.io/client-go/pkg/api/v1"
2422
"k8s.io/client-go/tools/cache"
2523
)
2624

@@ -31,17 +29,6 @@ type DeploymentsController struct {
3129
ClientGetter
3230
}
3331

34-
// ClientGetter creates an instances of clients used by this controller
35-
type ClientGetter interface {
36-
GetKubeClient(ctx context.Context) (kubernetes.KubeClientInterface, error)
37-
GetAndCheckOSIOClient(ctx context.Context) (OpenshiftIOClient, error)
38-
}
39-
40-
// Default implementation of KubeClientGetter and OSIOClientGetter used by NewDeploymentsController
41-
type defaultClientGetter struct {
42-
config *configuration.Registry
43-
}
44-
4532
// NewDeploymentsController creates a deployments controller.
4633
func NewDeploymentsController(service *goa.Service, config *configuration.Registry) *DeploymentsController {
4734
return &DeploymentsController{
@@ -61,41 +48,6 @@ func tostring(item interface{}) string {
6148
return string(bytes)
6249
}
6350

64-
func (g *defaultClientGetter) GetAndCheckOSIOClient(ctx context.Context) (OpenshiftIOClient, error) {
65-
66-
// defaults
67-
host := "localhost"
68-
scheme := "https"
69-
70-
req := goa.ContextRequest(ctx)
71-
if req != nil {
72-
// Note - it's probably more efficient to force a loopback host, and only use the port number here
73-
// (on some systems using a non-loopback interface forces a network stack traverse)
74-
host = req.Host
75-
scheme = req.URL.Scheme
76-
}
77-
78-
// The deployments API communicates with the rest of WIT via the stnadard WIT API.
79-
// This environment variable is used for local development of the deployments API, to point ot a remote WIT.
80-
witURLStr := os.Getenv("FABRIC8_WIT_API_URL")
81-
if witURLStr != "" {
82-
witurl, err := url.Parse(witURLStr)
83-
if err != nil {
84-
log.Error(ctx, map[string]interface{}{
85-
"FABRIC8_WIT_API_URL": witURLStr,
86-
"err": err,
87-
}, "cannot parse FABRIC8_WIT_API_URL: %s", witURLStr)
88-
return nil, errs.Wrapf(err, "cannot parse FABRIC8_WIT_API_URL: %s", witURLStr)
89-
}
90-
host = witurl.Host
91-
scheme = witurl.Scheme
92-
}
93-
94-
oc := NewOSIOClient(ctx, scheme, host)
95-
96-
return oc, nil
97-
}
98-
9951
// getSpaceNameFromSpaceID() converts an OSIO Space UUID to an OpenShift space name.
10052
// will return an error if the space is not found.
10153
func (c *DeploymentsController) getSpaceNameFromSpaceID(ctx context.Context, spaceID uuid.UUID) (*string, error) {
@@ -116,74 +68,6 @@ func (c *DeploymentsController) getSpaceNameFromSpaceID(ctx context.Context, spa
11668
return osioSpace.Attributes.Name, nil
11769
}
11870

119-
func (g *defaultClientGetter) getNamespaceName(ctx context.Context) (*string, error) {
120-
121-
osioclient, err := g.GetAndCheckOSIOClient(ctx)
122-
if err != nil {
123-
return nil, err
124-
}
125-
126-
kubeSpaceAttr, err := osioclient.GetNamespaceByType(ctx, nil, "user")
127-
if err != nil {
128-
return nil, errs.Wrap(err, "unable to retrieve 'user' namespace")
129-
}
130-
if kubeSpaceAttr == nil {
131-
return nil, errors.NewNotFoundError("namespace", "user")
132-
}
133-
134-
return kubeSpaceAttr.Name, nil
135-
}
136-
137-
// GetKubeClient creates a kube client for the appropriate cluster assigned to the current user
138-
func (g *defaultClientGetter) GetKubeClient(ctx context.Context) (kubernetes.KubeClientInterface, error) {
139-
140-
kubeNamespaceName, err := g.getNamespaceName(ctx)
141-
if err != nil {
142-
log.Error(ctx, map[string]interface{}{
143-
"err": err,
144-
}, "could not retrieve namespace name")
145-
return nil, errs.Wrap(err, "could not retrieve namespace name")
146-
}
147-
148-
osioclient, err := g.GetAndCheckOSIOClient(ctx)
149-
if err != nil {
150-
log.Error(ctx, map[string]interface{}{
151-
"err": err,
152-
}, "could not create OSIO client")
153-
return nil, err
154-
}
155-
156-
baseURLProvider, err := NewURLProvider(ctx, g.config, osioclient)
157-
if err != nil {
158-
log.Error(ctx, map[string]interface{}{
159-
"err": err,
160-
}, "could not retrieve tenant data")
161-
return nil, errs.Wrap(err, "could not retrieve tenant data")
162-
}
163-
164-
/* Timeout used per HTTP request to Kubernetes/OpenShift API servers.
165-
* Communication with Hawkular currently uses a hard-coded 30 second
166-
* timeout per request, and does not use this parameter. */
167-
// create the cluster API client
168-
kubeConfig := &kubernetes.KubeClientConfig{
169-
BaseURLProvider: baseURLProvider,
170-
UserNamespace: *kubeNamespaceName,
171-
Timeout: g.config.GetDeploymentsHTTPTimeoutSeconds(),
172-
}
173-
kc, err := kubernetes.NewKubeClient(kubeConfig)
174-
if err != nil {
175-
url, _ := baseURLProvider.GetAPIURL()
176-
log.Error(ctx, map[string]interface{}{
177-
"err": err,
178-
"user_namespace": *kubeNamespaceName,
179-
"cluster": *url,
180-
}, "could not create Kubernetes client object")
181-
return nil, errs.Wrap(err, "could not create Kubernetes client object")
182-
}
183-
return kc, nil
184-
}
185-
186-
// SetDeployment runs the setDeployment action.
18771
func (c *DeploymentsController) SetDeployment(ctx *app.SetDeploymentDeploymentsContext) error {
18872

18973
// we double check podcount here, because in the future we might have different query parameters

controller/deployments_blackbox_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ type testOSIOClient struct {
5353
controller.OpenshiftIOClient
5454
}
5555

56+
type testOSClient struct {
57+
fixture *deploymentsTestFixture
58+
kubernetes.OpenShiftRESTAPI
59+
}
60+
5661
func (kc *testKubeClient) Close() {
5762
kc.closed = true
5863
}
@@ -97,6 +102,12 @@ func (fixture *deploymentsTestFixture) GetAndCheckOSIOClient(ctx context.Context
97102
}, nil
98103
}
99104

105+
func (fixture *deploymentsTestFixture) GetOSClient(ctx context.Context) (kubernetes.OpenShiftRESTAPI, error) {
106+
return &testOSClient {
107+
fixture: fixture,
108+
}, nil
109+
}
110+
100111
func (c *testOSIOClient) GetSpaceByID(ctx context.Context, spaceID uuid.UUID) (*app.Space, error) {
101112
var spaceName *string
102113
uuidString := spaceID.String()

controller/pipelines.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package controller
2+
3+
import (
4+
"github.com/goadesign/goa"
5+
errs "github.com/pkg/errors"
6+
"github.com/fabric8-services/fabric8-wit/configuration"
7+
"github.com/fabric8-services/fabric8-wit/app"
8+
"github.com/fabric8-services/fabric8-wit/jsonapi"
9+
"github.com/fabric8-services/fabric8-wit/log"
10+
"github.com/fabric8-services/fabric8-wit/errors"
11+
)
12+
13+
// pipeline implements the pipeline resource.
14+
type PipelinesController struct {
15+
*goa.Controller
16+
Config *configuration.Registry
17+
ClientGetter
18+
}
19+
20+
func NewPipelineController(service *goa.Service, config *configuration.Registry) *PipelinesController {
21+
return &PipelinesController{
22+
Controller: service.NewController("PipelinesController"),
23+
Config: config,
24+
ClientGetter: &defaultClientGetter{
25+
config: config,
26+
},
27+
}
28+
}
29+
30+
// Delete a pipelines from given space
31+
func (c *PipelinesController) Delete(ctx *app.DeletePipelinesContext) error {
32+
33+
osioClient, err := c.GetAndCheckOSIOClient(ctx)
34+
if err != nil {
35+
return jsonapi.JSONErrorResponse(ctx, err)
36+
}
37+
38+
k8sSpace, err := osioClient.GetNamespaceByType(ctx, nil, "user")
39+
if err != nil {
40+
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, "unable to retrieve 'user' namespace"))
41+
}
42+
if k8sSpace == nil {
43+
return jsonapi.JSONErrorResponse(ctx, errors.NewNotFoundError("namespace", "user"))
44+
}
45+
46+
osc, err := c.GetOSClient(ctx)
47+
if err != nil {
48+
return jsonapi.JSONErrorResponse(ctx, err)
49+
}
50+
51+
userNS := *k8sSpace.Name
52+
resp, err := osc.DeleteBuildConfig(userNS, map[string]string{"space": ctx.Space})
53+
if err != nil {
54+
log.Error(ctx, map[string]interface{}{
55+
"err": err,
56+
"space_name": ctx.Space,
57+
}, "error occurred while deleting pipeline")
58+
return jsonapi.JSONErrorResponse(ctx, err)
59+
}
60+
61+
log.Info(ctx, map[string]interface{}{"response": resp}, "deleted pipelines :")
62+
63+
return ctx.OK([]byte{})
64+
}

0 commit comments

Comments
 (0)