This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docker image creation and deploy service calls to deploy created …
…image (#11)
- Loading branch information
1 parent
798e4b1
commit a53ceff
Showing
13 changed files
with
738 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package deploy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
|
||
"gofr.dev/pkg/gofr" | ||
|
||
kopsClient "kops.dev/client" | ||
"kops.dev/models" | ||
) | ||
|
||
const ( | ||
imageZipName = "temp/image.zip" | ||
) | ||
|
||
var ( | ||
errUpdatingImage = errors.New("unable to update the image for your service via kops.dev services") | ||
) | ||
|
||
type client struct { | ||
} | ||
|
||
func New() kopsClient.ServiceDeployer { | ||
return &client{} | ||
} | ||
|
||
func (*client) DeployImage(ctx *gofr.Context, img *models.Image) error { | ||
depSvc := ctx.GetHTTPService("deployment-service") | ||
|
||
body, header, err := getForm(img) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := depSvc.PostWithHeaders(ctx, "deploy", nil, body, header) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusCreated { | ||
ctx.Logger.Errorf("error communicating with the deployment service, status code returned - %d", resp.StatusCode) | ||
|
||
return errUpdatingImage | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getForm(img *models.Image) (bodyBytes []byte, headers map[string]string, err error) { | ||
file, err := os.Open(imageZipName) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
defer file.Close() | ||
|
||
body := &bytes.Buffer{} | ||
writer := multipart.NewWriter(body) | ||
|
||
defer writer.Close() | ||
|
||
part, err := writer.CreateFormFile("image", imageZipName) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
_, err = io.Copy(part, file) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
err = addField(writer, "name", img.Name) | ||
err = addField(writer, "tag", img.Tag) | ||
err = addField(writer, "region", img.Region) | ||
err = addField(writer, "repository", img.Repository) | ||
err = addField(writer, "serviceID", img.ServiceID) | ||
err = addField(writer, "repository", img.Repository) | ||
err = addField(writer, "region", img.Region) | ||
err = addField(writer, "loginServer", img.LoginServer) | ||
err = addField(writer, "serviceName", img.ServiceName) | ||
err = addField(writer, "accountID", img.AccountID) | ||
err = addField(writer, "cloudProvider", img.CloudProvider) | ||
|
||
creds, _ := writer.CreateFormField("serviceCreds") | ||
b, _ := json.Marshal(img.ServiceCreds) | ||
_, _ = creds.Write(b) | ||
|
||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
err = writer.Close() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return body.Bytes(), map[string]string{ | ||
"Content-Type": writer.FormDataContentType(), | ||
}, nil | ||
} | ||
|
||
func addField(writer *multipart.Writer, key, value string) error { | ||
if value == "" { | ||
return nil | ||
} | ||
|
||
return writer.WriteField(key, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package client | ||
|
||
import ( | ||
"gofr.dev/pkg/gofr" | ||
"kops.dev/models" | ||
) | ||
|
||
type ServiceDeployer interface { | ||
DeployImage(ctx *gofr.Context, img *models.Image) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.