Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit 1521616

Browse files
didiladibotchk
andauthored
Change the way the configuration service is configured (#10)
* Add improvements to the service This commit changes the way the configuration service URL is configured from the config.yaml of the service to a configuration in the helm chart. Additionally it: * adds the scapability to display kubernetes logs within the keptn bridge * makes the kubernetes jub run as non-root * adds some more logging to the service * fix deploy/service.yaml to work when deployed in the execution plane. Cleanup handling of env variables required by initcontainer. Fix tests and golint errors. * fix goling error, add comment to exported EventHandler * update reviewdog go version from 1.13 to 1.16 * fix gofmt and golint errors * Add comments in config.go the linting indicated * Fix reviewdog config for go vet Co-authored-by: Dominik Augustin <[email protected]>
1 parent 7c517f9 commit 1521616

File tree

21 files changed

+479
-203
lines changed

21 files changed

+479
-203
lines changed

.github/workflows/reviewdog.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
- name: Set up Go
99
uses: actions/setup-go@v2
1010
with:
11-
go-version: 1.13
11+
go-version: 1.16
1212
id: go
1313
- name: Check out code.
1414
uses: actions/checkout@v1

.reviewdog.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ runner:
99
errorformat:
1010
- "%f:%l %m"
1111
govet:
12-
cmd: go vet -all .
12+
cmd: go vet ./...

cmd/keptn-generic-job-service-initcontainer/main.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,19 @@ import (
1313
)
1414

1515
type envConfig struct {
16-
// Port on which to listen for cloudevents
17-
Port int `envconfig:"RCV_PORT" default:"8080"`
18-
// Path to which cloudevents are sent
19-
Path string `envconfig:"RCV_PATH" default:"/"`
2016
// Whether we are running locally (e.g., for testing) or on production
2117
Env string `envconfig:"ENV" default:"local"`
2218
// URL of the Keptn configuration service (this is where we can fetch files from the config repo)
23-
ConfigurationServiceUrl string `envconfig:"CONFIGURATION_SERVICE" default:""`
19+
ConfigurationServiceURL string `envconfig:"CONFIGURATION_SERVICE" required:"true"`
20+
// The token of the keptn API
21+
KeptnAPIToken string `envconfig:"KEPTN_API_TOKEN" required:"true"`
2422
// The keptn project contained in the initial cloud event
2523
Project string `envconfig:"KEPTN_PROJECT" required:"true"`
2624
// The keptn stage contained in the initial cloud event
2725
Stage string `envconfig:"KEPTN_STAGE" required:"true"`
2826
// The keptn service contained in the initial cloud event
2927
Service string `envconfig:"KEPTN_SERVICE" required:"true"`
3028
// The keptn service contained in the initial cloud event
31-
ApiToken string `envconfig:"KEPTN_API_TOKEN" required:"false"`
32-
// The name of the config action which triggered the init container run
3329
Action string `envconfig:"JOB_ACTION" required:"true"`
3430
// The name of the config task which triggered the init container run
3531
Task string `envconfig:"JOB_TASK" required:"true"`
@@ -45,11 +41,11 @@ func main() {
4541
fs := afero.NewOsFs()
4642

4743
var resourceHandler *api.ResourceHandler
48-
if env.ApiToken != "" {
49-
configurationServiceUrl, _ := url.Parse(env.ConfigurationServiceUrl)
50-
resourceHandler = api.NewAuthenticatedResourceHandler(configurationServiceUrl.String(), env.ApiToken, "x-token", nil, configurationServiceUrl.Scheme)
44+
if env.KeptnAPIToken != "" { // gets set as empty string from the generic-job-service if the env variable is not set
45+
configurationServiceURL, _ := url.Parse(env.ConfigurationServiceURL)
46+
resourceHandler = api.NewAuthenticatedResourceHandler(configurationServiceURL.String(), env.KeptnAPIToken, "x-token", nil, configurationServiceURL.Scheme)
5147
} else {
52-
resourceHandler = api.NewResourceHandler(env.ConfigurationServiceUrl)
48+
resourceHandler = api.NewResourceHandler(env.ConfigurationServiceURL)
5349
}
5450

5551
useLocalFileSystem := false
@@ -60,7 +56,7 @@ func main() {
6056
useLocalFileSystem = true
6157
}
6258

63-
configService := keptn.NewKeptnConfigService(useLocalFileSystem, env.Project, env.Stage, env.Service, resourceHandler)
59+
configService := keptn.NewConfigService(useLocalFileSystem, env.Project, env.Stage, env.Service, resourceHandler)
6460

6561
err := file.MountFiles(env.Action, env.Task, fs, configService)
6662
if err != nil {

cmd/keptn-generic-job-service/main.go

+19-17
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package main
33
import (
44
"context"
55
"didiladi/keptn-generic-job-service/pkg/eventhandler"
6-
"encoding/json"
76
"errors"
87
"log"
98
"os"
109
"strings"
1110

1211
cloudevents "github.com/cloudevents/sdk-go/v2" // make sure to use v2 cloudevents here
1312
"github.com/kelseyhightower/envconfig"
14-
keptn "github.com/keptn/go-utils/pkg/lib/keptn"
13+
"github.com/keptn/go-utils/pkg/lib/keptn"
1514
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
1615
)
1716

1817
var keptnOptions = keptn.KeptnOpts{}
18+
var env envConfig
1919

2020
type envConfig struct {
2121
// Port on which to listen for cloudevents
@@ -25,7 +25,13 @@ type envConfig struct {
2525
// Whether we are running locally (e.g., for testing) or on production
2626
Env string `envconfig:"ENV" default:"local"`
2727
// URL of the Keptn configuration service (this is where we can fetch files from the config repo)
28-
ConfigurationServiceUrl string `envconfig:"CONFIGURATION_SERVICE" default:""`
28+
ConfigurationServiceURL string `envconfig:"CONFIGURATION_SERVICE" default:""`
29+
// The endpoint of the keptn configuration service API
30+
InitContainerConfigurationServiceAPIEndpoint string `envconfig:"INIT_CONTAINER_CONFIGURATION_SERVICE_API_ENDPOINT" required:"true"`
31+
// The k8s namespace the job will run in
32+
JobNamespace string `envconfig:"JOB_NAMESPACE" required:"true"`
33+
// The token of the keptn API
34+
KeptnAPIToken string `envconfig:"KEPTN_API_TOKEN"`
2935
}
3036

3137
// ServiceName specifies the current services name (e.g., used as source when sending CloudEvents)
@@ -68,16 +74,18 @@ func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error
6874
log.Printf("failed to convert incoming cloudevent to event data: %v", err)
6975
}
7076

71-
var eventDataAsInterface interface{}
72-
//err = event.DataAs(eventDataAsInterface)
73-
err = json.Unmarshal(event.Data(), &eventDataAsInterface)
74-
//err = parseKeptnCloudEventPayload(event, eventDataAsInterface)
75-
if err != nil {
76-
log.Printf("failed to convert incoming cloudevent: %v", err)
77+
eventHandler := &eventhandler.EventHandler{
78+
Keptn: myKeptn,
79+
Event: event,
80+
EventData: eventData,
81+
ServiceName: ServiceName,
82+
JobNamespace: env.JobNamespace,
83+
InitContainerConfigurationServiceAPIEndpoint: env.InitContainerConfigurationServiceAPIEndpoint,
84+
KeptnAPIToken: env.KeptnAPIToken,
7785
}
7886

7987
// prevent duplicate events - https://github.com/keptn/keptn/issues/3888
80-
go eventhandler.HandleEvent(myKeptn, event, eventDataAsInterface, eventData, ServiceName)
88+
go eventHandler.HandleEvent()
8189

8290
return nil
8391
}
@@ -90,7 +98,6 @@ func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error
9098
* env=runlocal -> will fetch resources from local drive instead of configuration service
9199
*/
92100
func main() {
93-
var env envConfig
94101
if err := envconfig.Process("", &env); err != nil {
95102
log.Fatalf("Failed to process env var: %s", err)
96103
}
@@ -109,12 +116,7 @@ func _main(args []string, env envConfig) int {
109116
keptnOptions.UseLocalFileSystem = true
110117
}
111118

112-
_, available := os.LookupEnv("JOB_NAMESPACE")
113-
if !available {
114-
log.Fatalf("JOB_NAMESPACE was not available. Please set it as env var")
115-
}
116-
117-
keptnOptions.ConfigurationServiceURL = env.ConfigurationServiceUrl
119+
keptnOptions.ConfigurationServiceURL = env.ConfigurationServiceURL
118120

119121
log.Println("Starting keptn-generic-job-service...")
120122
log.Printf(" on Port = %d; Path=%s", env.Port, env.Path)

deploy/service.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ spec:
2121
ports:
2222
- containerPort: 8080
2323
env:
24+
- name: INIT_CONTAINER_CONFIGURATION_SERVICE_API_ENDPOINT
25+
value: "http://configuration-service:8080"
2426
- name: CONFIGURATION_SERVICE
2527
value: 'http://configuration-service:8080'
2628
- name: JOB_NAMESPACE
@@ -92,6 +94,7 @@ rules:
9294
- ""
9395
resources:
9496
- "pods"
97+
- "pods/log"
9598
- "persistentvolumeclaims"
9699
- "jobs"
97100
verbs:

go.mod

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module didiladi/keptn-generic-job-service
33
go 1.16
44

55
require (
6+
cloud.google.com/go v0.82.0 // indirect
7+
cloud.google.com/go/datastore v1.5.0 // indirect
68
github.com/PaesslerAG/jsonpath v0.1.1
79
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
810
github.com/cloudevents/sdk-go/v2 v2.4.1
@@ -12,23 +14,33 @@ require (
1214
github.com/go-openapi/strfmt v0.20.1 // indirect
1315
github.com/go-openapi/swag v0.19.15 // indirect
1416
github.com/go-openapi/validate v0.20.2 // indirect
15-
github.com/golang/mock v1.4.4
16-
github.com/golang/protobuf v1.5.2 // indirect
17+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
18+
github.com/golang/mock v1.5.0
19+
github.com/google/go-github/v29 v29.0.3 // indirect
20+
github.com/google/go-querystring v1.1.0 // indirect
1721
github.com/google/gofuzz v1.2.0 // indirect
1822
github.com/googleapis/gnostic v0.5.1 // indirect
23+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
24+
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
1925
github.com/json-iterator/go v1.1.11 // indirect
2026
github.com/kelseyhightower/envconfig v1.4.0
2127
github.com/keptn/go-utils v0.8.3
2228
github.com/keptn/kubernetes-utils v0.8.1
2329
github.com/mailru/easyjson v0.7.7 // indirect
30+
github.com/mattn/go-shellwords v1.0.11 // indirect
31+
github.com/reviewdog/errorformat v0.0.0-20210517100703-fd739bda5dda // indirect
32+
github.com/reviewdog/reviewdog v0.11.0 // indirect
2433
github.com/spf13/afero v1.2.2
34+
github.com/xanzy/go-gitlab v0.50.0 // indirect
2535
go.uber.org/multierr v1.6.0 // indirect
2636
go.uber.org/zap v1.16.0 // indirect
27-
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect
28-
golang.org/x/oauth2 v0.0.0-20210427180440-81ed05c6b58c // indirect
29-
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
37+
golang.org/x/build v0.0.0-20210518220400-d0819edf598a // indirect
38+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
39+
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
3040
golang.org/x/term v0.0.0-20210429154555-c04ba851c2a4 // indirect
31-
google.golang.org/appengine v1.6.7 // indirect
41+
google.golang.org/api v0.47.0 // indirect
42+
google.golang.org/genproto v0.0.0-20210518161634-ec7691c0a37d // indirect
43+
google.golang.org/grpc v1.38.0 // indirect
3244
gopkg.in/yaml.v2 v2.4.0
3345
gotest.tools v2.2.0+incompatible
3446
k8s.io/api v0.21.0

0 commit comments

Comments
 (0)