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

Commit a53ceff

Browse files
authored
Add docker image creation and deploy service calls to deploy created image (#11)
1 parent 798e4b1 commit a53ceff

File tree

13 files changed

+738
-185
lines changed

13 files changed

+738
-185
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
.DS_Store
78

89
# Test binary, built with `go test -c`
910
*.test
@@ -16,6 +17,8 @@ coverage.txt
1617
# Dependency directories (remove the comment below to include it)
1718
vendor/
1819

20+
test
21+
1922
# IDE cache and settings
2023
.idea
2124
.vscode

client/deploy/client.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package deploy
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"io"
8+
"mime/multipart"
9+
"net/http"
10+
"os"
11+
12+
"gofr.dev/pkg/gofr"
13+
14+
kopsClient "kops.dev/client"
15+
"kops.dev/models"
16+
)
17+
18+
const (
19+
imageZipName = "temp/image.zip"
20+
)
21+
22+
var (
23+
errUpdatingImage = errors.New("unable to update the image for your service via kops.dev services")
24+
)
25+
26+
type client struct {
27+
}
28+
29+
func New() kopsClient.ServiceDeployer {
30+
return &client{}
31+
}
32+
33+
func (*client) DeployImage(ctx *gofr.Context, img *models.Image) error {
34+
depSvc := ctx.GetHTTPService("deployment-service")
35+
36+
body, header, err := getForm(img)
37+
if err != nil {
38+
return err
39+
}
40+
41+
resp, err := depSvc.PostWithHeaders(ctx, "deploy", nil, body, header)
42+
if err != nil {
43+
return err
44+
}
45+
46+
defer resp.Body.Close()
47+
48+
if resp.StatusCode != http.StatusCreated {
49+
ctx.Logger.Errorf("error communicating with the deployment service, status code returned - %d", resp.StatusCode)
50+
51+
return errUpdatingImage
52+
}
53+
54+
return nil
55+
}
56+
57+
func getForm(img *models.Image) (bodyBytes []byte, headers map[string]string, err error) {
58+
file, err := os.Open(imageZipName)
59+
if err != nil {
60+
return nil, nil, err
61+
}
62+
63+
defer file.Close()
64+
65+
body := &bytes.Buffer{}
66+
writer := multipart.NewWriter(body)
67+
68+
defer writer.Close()
69+
70+
part, err := writer.CreateFormFile("image", imageZipName)
71+
if err != nil {
72+
return nil, nil, err
73+
}
74+
75+
_, err = io.Copy(part, file)
76+
if err != nil {
77+
return nil, nil, err
78+
}
79+
80+
err = addField(writer, "name", img.Name)
81+
err = addField(writer, "tag", img.Tag)
82+
err = addField(writer, "region", img.Region)
83+
err = addField(writer, "repository", img.Repository)
84+
err = addField(writer, "serviceID", img.ServiceID)
85+
err = addField(writer, "repository", img.Repository)
86+
err = addField(writer, "region", img.Region)
87+
err = addField(writer, "loginServer", img.LoginServer)
88+
err = addField(writer, "serviceName", img.ServiceName)
89+
err = addField(writer, "accountID", img.AccountID)
90+
err = addField(writer, "cloudProvider", img.CloudProvider)
91+
92+
creds, _ := writer.CreateFormField("serviceCreds")
93+
b, _ := json.Marshal(img.ServiceCreds)
94+
_, _ = creds.Write(b)
95+
96+
if err != nil {
97+
return nil, nil, err
98+
}
99+
100+
err = writer.Close()
101+
if err != nil {
102+
return nil, nil, err
103+
}
104+
105+
return body.Bytes(), map[string]string{
106+
"Content-Type": writer.FormDataContentType(),
107+
}, nil
108+
}
109+
110+
func addField(writer *multipart.Writer, key, value string) error {
111+
if value == "" {
112+
return nil
113+
}
114+
115+
return writer.WriteField(key, value)
116+
}

client/interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package client
2+
3+
import (
4+
"gofr.dev/pkg/gofr"
5+
"kops.dev/models"
6+
)
7+
8+
type ServiceDeployer interface {
9+
DeployImage(ctx *gofr.Context, img *models.Image) error
10+
}

go.mod

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,113 @@ module kops.dev
22

33
go 1.22
44

5-
require gofr.dev v1.15.0
5+
require (
6+
github.com/docker/docker v27.2.0+incompatible
7+
gofr.dev v1.19.0
8+
golang.org/x/sync v0.8.0
9+
)
610

711
require (
8-
cloud.google.com/go v0.115.0 // indirect
9-
cloud.google.com/go/auth v0.7.0 // indirect
10-
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
11-
cloud.google.com/go/compute/metadata v0.4.0 // indirect
12-
cloud.google.com/go/iam v1.1.10 // indirect
13-
cloud.google.com/go/pubsub v1.40.0 // indirect
12+
cloud.google.com/go v0.115.1 // indirect
13+
cloud.google.com/go/auth v0.9.1 // indirect
14+
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
15+
cloud.google.com/go/compute/metadata v0.5.0 // indirect
16+
cloud.google.com/go/iam v1.1.13 // indirect
17+
cloud.google.com/go/pubsub v1.42.0 // indirect
1418
filippo.io/edwards25519 v1.1.0 // indirect
19+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
1520
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
16-
github.com/XSAM/otelsql v0.32.0 // indirect
21+
github.com/Microsoft/go-winio v0.4.14 // indirect
22+
github.com/XSAM/otelsql v0.33.0 // indirect
1723
github.com/beorn7/perks v1.0.1 // indirect
1824
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
1925
github.com/cespare/xxhash/v2 v2.3.0 // indirect
26+
github.com/containerd/log v0.1.0 // indirect
2027
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
28+
github.com/distribution/reference v0.6.0 // indirect
29+
github.com/docker/go-connections v0.5.0 // indirect
30+
github.com/docker/go-units v0.5.0 // indirect
2131
github.com/dustin/go-humanize v1.0.1 // indirect
22-
github.com/eclipse/paho.mqtt.golang v1.4.3 // indirect
32+
github.com/eclipse/paho.mqtt.golang v1.5.0 // indirect
2333
github.com/felixge/httpsnoop v1.0.4 // indirect
2434
github.com/go-logr/logr v1.4.2 // indirect
2535
github.com/go-logr/stdr v1.2.2 // indirect
2636
github.com/go-sql-driver/mysql v1.8.1 // indirect
2737
github.com/gogo/protobuf v1.3.2 // indirect
2838
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
2939
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
30-
github.com/golang/protobuf v1.5.4 // indirect
31-
github.com/google/s2a-go v0.1.7 // indirect
40+
github.com/google/s2a-go v0.1.8 // indirect
3241
github.com/google/uuid v1.6.0 // indirect
3342
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
34-
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
43+
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
3544
github.com/gorilla/mux v1.8.1 // indirect
3645
github.com/gorilla/websocket v1.5.3 // indirect
3746
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
38-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
47+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
3948
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
4049
github.com/joho/godotenv v1.5.1 // indirect
41-
github.com/klauspost/compress v1.17.8 // indirect
50+
github.com/klauspost/compress v1.17.9 // indirect
4251
github.com/lib/pq v1.10.9 // indirect
4352
github.com/mattn/go-isatty v0.0.20 // indirect
53+
github.com/moby/docker-image-spec v1.3.1 // indirect
54+
github.com/moby/patternmatcher v0.6.0 // indirect
55+
github.com/moby/sys/sequential v0.6.0 // indirect
56+
github.com/moby/sys/user v0.3.0 // indirect
57+
github.com/moby/sys/userns v0.1.0 // indirect
58+
github.com/moby/term v0.5.0 // indirect
59+
github.com/morikuni/aec v1.0.0 // indirect
4460
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4561
github.com/ncruces/go-strftime v0.1.9 // indirect
62+
github.com/opencontainers/go-digest v1.0.0 // indirect
63+
github.com/opencontainers/image-spec v1.1.0 // indirect
4664
github.com/openzipkin/zipkin-go v0.4.3 // indirect
4765
github.com/pierrec/lz4/v4 v4.1.21 // indirect
4866
github.com/pkg/errors v0.9.1 // indirect
49-
github.com/prometheus/client_golang v1.19.1 // indirect
67+
github.com/prometheus/client_golang v1.20.2 // indirect
5068
github.com/prometheus/client_model v0.6.1 // indirect
5169
github.com/prometheus/common v0.55.0 // indirect
5270
github.com/prometheus/procfs v0.15.1 // indirect
5371
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
5472
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 // indirect
55-
github.com/redis/go-redis/v9 v9.5.4 // indirect
73+
github.com/redis/go-redis/v9 v9.6.1 // indirect
5674
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
5775
github.com/segmentio/kafka-go v0.4.47 // indirect
76+
github.com/sirupsen/logrus v1.9.3 // indirect
5877
go.opencensus.io v0.24.0 // indirect
5978
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
6079
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.53.0 // indirect
6180
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
62-
go.opentelemetry.io/otel v1.28.0 // indirect
63-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect
64-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0 // indirect
65-
go.opentelemetry.io/otel/exporters/prometheus v0.50.0 // indirect
66-
go.opentelemetry.io/otel/exporters/zipkin v1.28.0 // indirect
67-
go.opentelemetry.io/otel/metric v1.28.0 // indirect
68-
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
69-
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
70-
go.opentelemetry.io/otel/trace v1.28.0 // indirect
71-
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
81+
go.opentelemetry.io/otel v1.29.0 // indirect
82+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect
83+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 // indirect
84+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect
85+
go.opentelemetry.io/otel/exporters/prometheus v0.51.0 // indirect
86+
go.opentelemetry.io/otel/exporters/zipkin v1.29.0 // indirect
87+
go.opentelemetry.io/otel/metric v1.29.0 // indirect
88+
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
89+
go.opentelemetry.io/otel/sdk/metric v1.29.0 // indirect
90+
go.opentelemetry.io/otel/trace v1.29.0 // indirect
91+
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
7292
go.uber.org/mock v0.4.0 // indirect
73-
golang.org/x/crypto v0.25.0 // indirect
74-
golang.org/x/net v0.27.0 // indirect
75-
golang.org/x/oauth2 v0.21.0 // indirect
76-
golang.org/x/sync v0.7.0 // indirect
77-
golang.org/x/sys v0.22.0 // indirect
78-
golang.org/x/term v0.22.0 // indirect
79-
golang.org/x/text v0.16.0 // indirect
80-
golang.org/x/time v0.5.0 // indirect
81-
google.golang.org/api v0.188.0 // indirect
82-
google.golang.org/genproto v0.0.0-20240708141625-4ad9e859172b // indirect
83-
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
84-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
85-
google.golang.org/grpc v1.65.0 // indirect
93+
golang.org/x/crypto v0.26.0 // indirect
94+
golang.org/x/net v0.28.0 // indirect
95+
golang.org/x/oauth2 v0.22.0 // indirect
96+
golang.org/x/sys v0.24.0 // indirect
97+
golang.org/x/term v0.23.0 // indirect
98+
golang.org/x/text v0.17.0 // indirect
99+
golang.org/x/time v0.6.0 // indirect
100+
google.golang.org/api v0.195.0 // indirect
101+
google.golang.org/genproto v0.0.0-20240823204242-4ba0660f739c // indirect
102+
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
103+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240823204242-4ba0660f739c // indirect
104+
google.golang.org/grpc v1.66.0 // indirect
86105
google.golang.org/protobuf v1.34.2 // indirect
106+
gotest.tools/v3 v3.5.1 // indirect
87107
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
88-
modernc.org/libc v1.52.1 // indirect
108+
modernc.org/libc v1.55.3 // indirect
89109
modernc.org/mathutil v1.6.0 // indirect
90110
modernc.org/memory v1.8.0 // indirect
91-
modernc.org/sqlite v1.30.2 // indirect
111+
modernc.org/sqlite v1.32.0 // indirect
92112
modernc.org/strutil v1.2.0 // indirect
93113
modernc.org/token v1.1.0 // indirect
94114
)

0 commit comments

Comments
 (0)