-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagefile.go
117 lines (101 loc) · 2.71 KB
/
Magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//go:build mage
// +build mage
package main
import (
"context"
"errors"
"io/ioutil"
"os"
"path"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/otiai10/copy"
// mage:import
"github.com/wavesoftware/go-magetasks"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/pkg/artifact"
"github.com/wavesoftware/go-magetasks/pkg/artifact/platform"
"github.com/wavesoftware/go-magetasks/pkg/checks"
"github.com/wavesoftware/go-magetasks/pkg/files"
"github.com/wavesoftware/go-magetasks/pkg/git"
"github.com/wavesoftware/k8s-aware/pkg/metadata"
)
// Default target is set to binary.
//goland:noinspection GoUnusedGlobalVariable
var Default = magetasks.Build // nolint:deadcode,gochecknoglobals
func init() { //nolint:gochecknoinits
im := artifact.Image{
Metadata: config.Metadata{Name: "service"},
Architectures: []platform.Architecture{platform.AMD64},
}
magetasks.Configure(config.Config{
Version: &config.Version{
Path: metadata.VersionPath(),
Resolver: git.NewVersionResolver(),
},
Artifacts: []config.Artifact{im},
Checks: []config.Task{checks.GolangCiLint()},
Context: context.WithValue(context.TODO(),
key{"image"}, artifact.ImageReferenceOf(im)),
})
}
// Deploy will deploy service on a cluster.
// goland:noinspection GoUnusedExportedFunction
// nolint:deadcode
func Deploy() error {
mg.Deps(magetasks.Publish)
files.EnsureBuildDir()
source := path.Join(files.ProjectDir(), "deploy")
target := path.Join(files.BuildDir(), "deploy")
err := copy.Copy(source, target)
if err != nil {
return err // nolint:wrapcheck
}
return withinDirectory(target, deployWithKubectl)
}
func deployWithKubectl() error {
resolver, ok := config.Actual().Context.Value(key{"image"}).(config.Resolver)
if !ok {
return errNoResolver
}
err := replaceImage(resolver)
if err != nil {
return err
}
err = sh.RunV("kubectl", "apply", "-f", ".")
if err != nil {
return err // nolint:wrapcheck
}
return nil
}
func replaceImage(resolver config.Resolver) error {
input, err := ioutil.ReadFile("100-service.yaml")
if err != nil {
return err // nolint:wrapcheck
}
contents := strings.ReplaceAll(string(input),
"ghcr.io/wavesoftware/k8s-aware/service:latest",
resolver())
err = ioutil.WriteFile("100-service.yaml", []byte(contents), rwUser)
return err // nolint:wrapcheck
}
func withinDirectory(path string, fn func() error) error {
dir, err := os.Getwd()
if err != nil {
return err // nolint:wrapcheck
}
err = os.Chdir(path)
if err != nil {
return err // nolint:wrapcheck
}
defer func() {
_ = os.Chdir(dir)
}()
return fn()
}
const rwUser = 0o600
var errNoResolver = errors.New("no resolver")
type key struct {
name string
}