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

Commit

Permalink
Merge pull request #12 from kops-dev/release-v0.1.0
Browse files Browse the repository at this point in the history
Release v0.1.0
  • Loading branch information
vipul-rawat authored Sep 5, 2024
2 parents 0f74f2d + 11f2efe commit a35398d
Show file tree
Hide file tree
Showing 19 changed files with 971 additions and 465 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.21', '1.22']
go-version: ['1.22', '1.21']

steps:
- name: Checkout code into go module directory
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
id: Go

- name: Get dependencies
run: |
go mod download
go mod tidy
- name: Test
run: |
Expand All @@ -51,13 +54,13 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Go 1.21
- name: Install Go 1.22
uses: actions/setup-go@v4
with:
go-version: 1.21
go-version: 1.22

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: v1.57.2
version: v1.59.1
args: --timeout=9m
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
.DS_Store

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

test

# IDE cache and settings
.idea
.vscode
56 changes: 50 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
linters-settings:
dupl:
threshold: 100
Expand All @@ -8,7 +9,9 @@ linters-settings:
statements: 50
gci:
sections:
- prefix(kops.dev)
- standard
- default
- localmodule
goconst:
min-len: 2
min-occurrences: 2
Expand All @@ -28,10 +31,10 @@ linters-settings:
gocyclo:
min-complexity: 10
goimports:
local-prefixes: kops.dev
local-prefixes: github.com/golangci/golangci-lint
golint:
min-confidence: 0
gomnd:
mnd:
checks:
- argument
- case
Expand All @@ -40,6 +43,11 @@ linters-settings:
govet:
enable:
- shadow
settings:
printf:
funcs:
- (gofr.dev/pkg/gofr/Logger).Logf
- (gofr.dev/pkg/gofr/Logger).Errorf
lll:
line-length: 140
maligned:
Expand All @@ -50,6 +58,38 @@ linters-settings:
allow-unused: false # report any unused nolint directives
require-explanation: true # require an explanation for nolint directives
require-specific: true # require nolint directives to be specific about which linter is being skipped
revive:
rules:
# default revive rules, they have to be present otherwise they are disabled
- name: blank-imports
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: empty-block
- name: error-naming
- name: error-return
- name: error-strings
- name: errorf
- name: increment-decrement
- name: indent-error-flow
- name: range
- name: receiver-naming
- name: redefines-builtin-id
- name: superfluous-else
- name: time-naming
- name: unexported-return
- name: unreachable-code
- name: unused-parameter
- name: var-declaration
- name: var-naming
# additional revive rules
- name: bare-return
- name: bool-literal-in-expr
- name: comment-spacings
- name: early-return
- name: defer
- name: deep-exit
- name: unused-receiver

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
Expand All @@ -60,7 +100,9 @@ linters:
- bodyclose
- dogsled
- dupl
- err113
- errcheck
- errorlint
- exhaustive
- exportloopref
- funlen
Expand All @@ -71,17 +113,17 @@ linters:
- gocritic
- gocyclo
- godot
- goerr113
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- mirror
- misspell
- mnd
- nakedret
- nestif
- noctx
Expand All @@ -91,9 +133,11 @@ linters:
- rowserrcheck
- staticcheck
- stylecheck
- thelper
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
- wsl

Expand All @@ -103,7 +147,7 @@ linters:


service:
golangci-lint-version: 1.58.x
golangci-lint-version: 1.59.x

issues:
# exclude-use-default: false
Expand Down
116 changes: 116 additions & 0 deletions client/deploy/client.go
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)
}
10 changes: 10 additions & 0 deletions client/interface.go
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
}
Loading

0 comments on commit a35398d

Please sign in to comment.