Skip to content

Commit

Permalink
Merge pull request #86 from GSA-TTS/zjr/stage-common
Browse files Browse the repository at this point in the history
Common Stage Base (and friends)
  • Loading branch information
zjrgov authored Feb 4, 2025
2 parents 08a76f6 + 1cb1ef4 commit 19f0059
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 37 deletions.
21 changes: 19 additions & 2 deletions runner-manager/cfd/cloudgov/cf_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,32 @@ func (cf *CFClientAPI) conn() *client.Client {
panic("go-cfclient adapter is not connected")
}

func castApp(app *resource.App) *App {
return &(App{app.GUID, app.Name, app.State})
}

func castApps(apps []*resource.App) []*App {
Apps := make([]*App, len(apps))
for idx, app := range apps {
Apps[idx] = &(App{app.GUID, app.Name, app.State})
Apps[idx] = castApp(app)
}
return Apps
}

func (cf *CFClientAPI) appsGet() ([]*App, error) {
func (cf *CFClientAPI) appGet(id string) (*App, error) {
app, err := cf.conn().Applications.Get(context.Background(), id)
if err != nil {
return nil, err
}
return castApp(app), nil
}

func (cf *CFClientAPI) appDelete(id string) error {
_, err := cf.conn().Applications.Delete(context.Background(), id)
return err
}

func (cf *CFClientAPI) appsList() ([]*App, error) {
apps, err := cf.conn().Applications.ListAll(context.Background(), nil)
if err != nil {
return nil, err
Expand Down
18 changes: 14 additions & 4 deletions runner-manager/cfd/cloudgov/cloudgov.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ package cloudgov
type ClientAPI interface {
connect(url string, creds *Creds) error

appsGet() (apps []*App, err error)
appGet(id string) (*App, error)
appDelete(id string) error
appsList() (apps []*App, err error)
}

type CredsGetter interface {
Expand All @@ -39,7 +41,7 @@ func New(i ClientAPI, o *Opts) (*Client, error) {
if o == nil {
o = &Opts{CredsGetter: EnvCredsGetter{}}
}
cg := &Client{i, o}
cg := &Client{ClientAPI: i, Opts: o}
return cg.Connect()
}

Expand Down Expand Up @@ -74,6 +76,14 @@ type App struct {
State string
}

func (c *Client) AppsGet() ([]*App, error) {
return c.appsGet()
func (c *Client) AppGet(id string) (*App, error) {
return c.appGet(id)
}

func (c *Client) AppDelete(id string) error {
return c.appDelete(id)
}

func (c *Client) AppsList() ([]*App, error) {
return c.appsList()
}
6 changes: 3 additions & 3 deletions runner-manager/cfd/cloudgov/cloudgov_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func Test_CFAdapter_AppsGet(t *testing.T) {
func Test_CFAdapter_AppGet(t *testing.T) {
var u, p, want string
var l int

Expand Down Expand Up @@ -68,9 +68,9 @@ scanning:
return
}

apps, err := cgClient.AppsGet()
apps, err := cgClient.AppsList()
if err != nil {
t.Errorf("Error running AppsGet() = %v", err)
t.Errorf("Error running AppsList() = %v", err)
return
}

Expand Down
16 changes: 8 additions & 8 deletions runner-manager/cfd/cloudgov/cloudgov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type stubClientAPI struct {
StCreds *Creds
StApps []*App

FailAppsGet bool
FailConnect bool
FailAppsList bool
FailConnect bool
}

func (a *stubClientAPI) appsGet() (apps []*App, err error) {
if a.FailAppsGet {
func (a *stubClientAPI) appsList() (apps []*App, err error) {
if a.FailAppsList {
return nil, errors.New("fail")
}
return a.StApps, nil
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestClient_Connect(t *testing.T) {
}
}

func TestClient_AppsGet(t *testing.T) {
func TestClient_AppsList(t *testing.T) {
testApps := []*App{{Id: "1", Name: "foo"}}

type fields struct {
Expand All @@ -260,7 +260,7 @@ func TestClient_AppsGet(t *testing.T) {
name: "reports errors",
wantErr: true,
fields: fields{
ClientAPI: &stubClientAPI{StApps: testApps, FailAppsGet: true},
ClientAPI: &stubClientAPI{StApps: testApps, FailAppsList: true},
Opts: &Opts{CredsGetter: &stubCredsGetter{}},
},
},
Expand All @@ -280,9 +280,9 @@ func TestClient_AppsGet(t *testing.T) {
ClientAPI: tt.fields.ClientAPI,
Opts: tt.fields.Opts,
}
got, err := c.AppsGet()
got, err := c.AppsList()
if (err != nil) != tt.wantErr {
t.Errorf("Client.AppsGet() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Client.AppsList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
Expand Down
8 changes: 6 additions & 2 deletions runner-manager/cfd/cloudgov/testdata/.cloudgov_creds.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# For simple integration test with cloudgov_integration_test.go
#
# This test will use the credentials provided to run `cf apps` and
# check the output against what you have provided.
#
# 1. copy this file to `.cloudgov_creds`
# 2. replace with real credentials
# 3. replace with real output
# 2. replace with real credentials, e.g. a service key's
# 3. replace with real output from `cf apps`
username-1234-asdf
password-asdf-1234
I am the expected output!
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,28 @@ func (c *JobConfig) parseEnv() *JobConfig {
return c
}

func GetJobConfig() *JobConfig {
cfg := (&JobConfig{}).parseEnv()
func getJobConfig() (cfg *JobConfig, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error getting job config: %w", err)
}
}()

cfg = (&JobConfig{}).parseEnv()

if err := cfg.parseJobResponseFile(); err != nil {
panic(err)
if err = cfg.parseJobResponseFile(); err != nil {
return nil, err
}
if err := cfg.parseVcapAppJSON(); err != nil {
panic(err)
if err = cfg.parseVcapAppJSON(); err != nil {
return nil, err
}

cfg.ContainerID = fmt.Sprintf(
"glrw-r%v-p%v-c%v-j%v",
cfg.RunnerID,
"glrw-p%v-c%v-j%v",
cfg.ProjectID,
cfg.ConcurrentProjectID,
cfg.JobID,
)

return cfg
return cfg, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Test_GetJobConfig(t *testing.T) {
WorkerDiskSize: "1024M",
JobResponseFile: "",
VcapAppJSON: "",
ContainerID: "glrw-r-p-c-j",
ContainerID: "glrw-p-c-j",
}

envVarsToSet := map[string]string{
Expand All @@ -36,7 +36,11 @@ func Test_GetJobConfig(t *testing.T) {
for k, v := range envVarsToSet {
t.Setenv(k, v)
}
parsedCfg := GetJobConfig()
parsedCfg, err := getJobConfig()
if err != nil {
t.Error(err)
return
}
if diff := cmp.Diff(cfgWant, parsedCfg); diff != "" {
t.Error(diff)
}
Expand All @@ -62,7 +66,11 @@ func Test_parseJobResponseFile(t *testing.T) {
Variables: []*CIVar{{Key: "foo", Value: "bar"}},
}

cfg := GetJobConfig()
cfg, err := getJobConfig()
if err != nil {
t.Error(err)
return
}

if diff := cmp.Diff(wanted, cfg.JobResponse); diff != "" {
t.Error(diff)
Expand All @@ -78,7 +86,11 @@ func Test_parseVcapAppJSON(t *testing.T) {
SpaceName: "zjr-gl-test",
}

cfg := GetJobConfig()
cfg, err := getJobConfig()
if err != nil {
t.Error(err)
return
}

if diff := cmp.Diff(wanted, cfg.VcapAppData); diff != "" {
t.Error(diff)
Expand Down
75 changes: 71 additions & 4 deletions runner-manager/cfd/cmd/drive/prepare.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package drive

import (
"log"
"fmt"

"github.com/spf13/cobra"
)
Expand All @@ -28,7 +28,74 @@ job log.
Read more in GitLab's documentation:
https://docs.gitlab.com/runner/executors/custom.html#prepare`,
Run: func(cmd *cobra.Command, args []string) {
log.Println("preparing...")
},
Run: run,
}

type prepStage commonStage

func run(cmd *cobra.Command, args []string) {
// Move this stuff into a setup, add methods.
s, err := newStage()
if err != nil {
panic(fmt.Errorf("error getting cgClient: %w", err))
}

s.prep.startServices()

// if services, start services

// if os.Getenv("")

// create temp manifest

// start container

// install deps

// allow access to services
}

func (s *prepStage) startServices() error {
if len(s.config.Services) < 1 {
return nil
}

for _, serv := range s.config.Services {
containerID := fmt.Sprintf("%v-svc-%v", s.config.ContainerID, serv.Alias)

// check for an old instance of the service, delete if found
app, err := s.client.AppGet(containerID)
if err != nil {
return fmt.Errorf("error checking for existing service (%v): %w", containerID, err)
}
if app != nil {
err = s.client.AppDelete(containerID)
}
if err != nil {
return fmt.Errorf("error deleting existing service (%v): %w", containerID, err)
}

// args =
// guid
// worker_mem
// docker image
// health check type
// no-route
//
// add job-vars & service-vars
// (we add to $SVCMANIFEST right now, maybe a way around that?)
//
// add entrypoint & command
//
// add docker user/pass
//
// push
//
// map-route containerID apps.internal --hostname containerID
//
// export CI_SERVICE_$alias=$containerID.apps.internal
//
}

return nil
}
46 changes: 46 additions & 0 deletions runner-manager/cfd/cmd/drive/stage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package drive

import (
"fmt"

"github.com/GSA-TTS/gitlab-runner-cloudgov/runner/cfd/cloudgov"
)

type stage struct {
// conf
prep *prepStage
// run
// clean

common commonStage
}

type commonStage struct {
client *cloudgov.Client
config *JobConfig
}

func newStage() (s *stage, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("error creating stage: %w", err)
}
}()

s.common.client, err = cloudgov.New(&cloudgov.CFClientAPI{}, nil)
if err != nil {
return
}

s.common.config, err = getJobConfig()
if err != nil {
return
}

// conf
s.prep = (*prepStage)(&s.common)
// run
// clean

return
}
2 changes: 1 addition & 1 deletion runner-manager/cfd/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/GSA-TTS/gitlab-runner-cloudgov/runner/cfd

go 1.23
go 1.23.5

require (
github.com/cloudfoundry/go-cfclient/v3 v3.0.0-alpha.9
Expand Down

0 comments on commit 19f0059

Please sign in to comment.