Skip to content

Commit 80caab2

Browse files
committed
ansible and helm migrations use go.mod
1 parent bd9a262 commit 80caab2

File tree

17 files changed

+287
-471
lines changed

17 files changed

+287
-471
lines changed

Diff for: commands/operator-sdk/cmd/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func buildFunc(cmd *cobra.Command, args []string) error {
212212
case projutil.OperatorTypeHelm:
213213
return fmt.Errorf("test scaffolding for Helm Operators is not implemented")
214214
default:
215-
return fmt.Errorf("unknown operator type '%v'", t)
215+
return &projutil.ErrUnknownOperatorType{t}
216216
}
217217

218218
if err != nil {

Diff for: commands/operator-sdk/cmd/migrate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func migrateAnsible() error {
8686
s := &scaffold.Scaffold{}
8787
err = s.Execute(cfg,
8888
&ansible.Main{},
89-
&ansible.GopkgToml{},
89+
&ansible.GoMod{},
9090
&dockerfile,
9191
&ansible.Entrypoint{},
9292
&ansible.UserSetup{},
@@ -114,7 +114,7 @@ func migrateHelm() error {
114114
s := &scaffold.Scaffold{}
115115
err := s.Execute(cfg,
116116
&helm.Main{},
117-
&helm.GopkgToml{},
117+
&helm.GoMod{},
118118
&helm.DockerfileHybrid{
119119
Watches: true,
120120
HelmCharts: true,

Diff for: go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module github.com/operator-framework/operator-sdk
22

33
require (
44
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
5-
github.com/BurntSushi/toml v0.3.1
65
github.com/MakeNowJust/heredoc v0.0.0-20171113091838-e9091a26100e // indirect
76
github.com/Masterminds/semver v1.4.2 // indirect
87
github.com/Masterminds/sprig v2.16.0+incompatible // indirect

Diff for: internal/gomod/print.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,32 @@ import (
1919
"fmt"
2020
"text/tabwriter"
2121

22+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
23+
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
24+
"github.com/operator-framework/operator-sdk/pkg/scaffold/helm"
2225
"github.com/operator-framework/operator-sdk/pkg/scaffold/project"
2326
)
2427

2528
func PrintDepsAsFile() error {
26-
b, err := project.ReadDepsFile()
29+
var (
30+
b []byte
31+
err error
32+
t = projutil.GetOperatorType()
33+
)
34+
switch t {
35+
case projutil.OperatorTypeGo:
36+
b, err = project.ReadDepsFile()
37+
case projutil.OperatorTypeAnsible:
38+
b, err = ansible.ReadDepsFile()
39+
case projutil.OperatorTypeHelm:
40+
b, err = helm.ReadDepsFile()
41+
default:
42+
return &projutil.ErrUnknownOperatorType{t}
43+
}
2744
if err != nil {
2845
return err
2946
}
47+
3048
fmt.Println(string(b))
3149
return nil
3250
}

Diff for: internal/util/fileutil/file_util.go

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const (
3434
DefaultExecFileMode = 0755
3535

3636
DefaultFileFlags = os.O_WRONLY | os.O_CREATE
37+
38+
// Separator to statically create directories.
39+
PathSep = string(filepath.Separator)
3740
)
3841

3942
// FileWriter is a io wrapper to write files

Diff for: internal/util/projutil/project_util.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ import (
2121
"path/filepath"
2222
"strings"
2323

24-
"github.com/operator-framework/operator-sdk/pkg/scaffold"
25-
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
26-
"github.com/operator-framework/operator-sdk/pkg/scaffold/helm"
24+
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
2725

2826
log "github.com/sirupsen/logrus"
2927
"github.com/spf13/cobra"
3028
)
3129

3230
const (
33-
GopathEnv = "GOPATH"
34-
SrcDir = "src"
31+
GopathEnv = "GOPATH"
32+
SrcDir = "src"
33+
mainFile = "cmd" + fileutil.PathSep + "manager" + fileutil.PathSep + "main.go"
34+
rolesDir = "roles"
35+
helmChartsDir = "helm-charts"
36+
buildDockerfile = "build" + fileutil.PathSep + "Dockerfile"
3537
)
3638

37-
var mainFile = filepath.Join(scaffold.ManagerDir, scaffold.CmdFile)
38-
3939
// OperatorType - the type of operator
4040
type OperatorType = string
4141

@@ -50,15 +50,23 @@ const (
5050
OperatorTypeUnknown OperatorType = "unknown"
5151
)
5252

53-
// MustInProjectRoot checks if the current dir is the project root and returns the current repo's import path
54-
// e.g github.com/example-inc/app-operator
53+
type ErrUnknownOperatorType struct {
54+
Typ string
55+
}
56+
57+
func (e *ErrUnknownOperatorType) Error() string {
58+
return fmt.Sprintf("unknown operator type '%v'", e.Typ)
59+
}
60+
61+
// MustInProjectRoot checks if the current dir is the project root and returns
62+
// the current repo's import path, e.g. github.com/example-inc/app-operator
5563
func MustInProjectRoot() {
56-
// if the current directory has the "./build/dockerfile" file, then it is safe to say
64+
// if the current directory has the buildDockerfile, then it is safe to say
5765
// we are at the project root.
58-
_, err := os.Stat(filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile))
66+
_, err := os.Stat(buildDockerfile)
5967
if err != nil {
6068
if os.IsNotExist(err) {
61-
log.Fatal("Must run command in project root dir: project structure requires ./build/Dockerfile")
69+
log.Fatalf("Must run command in project root dir: project structure requires %s", buildDockerfile)
6270
}
6371
log.Fatalf("Error while checking if current directory is the project root: (%v)", err)
6472
}
@@ -101,10 +109,10 @@ func GetOperatorType() OperatorType {
101109
if _, err := os.Stat(mainFile); err == nil {
102110
return OperatorTypeGo
103111
}
104-
if stat, err := os.Stat(ansible.RolesDir); err == nil && stat.IsDir() {
112+
if stat, err := os.Stat(rolesDir); err == nil && stat.IsDir() {
105113
return OperatorTypeAnsible
106114
}
107-
if stat, err := os.Stat(helm.HelmChartsDir); err == nil && stat.IsDir() {
115+
if stat, err := os.Stat(helmChartsDir); err == nil && stat.IsDir() {
108116
return OperatorTypeHelm
109117
}
110118
return OperatorTypeUnknown

Diff for: pkg/scaffold/ansible/go_mod.go

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2019 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ansible
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"html/template"
21+
22+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
23+
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
24+
)
25+
26+
const GoModFile = "go.mod"
27+
28+
// GoMod - the go.mod file for an Ansible hybrid operator.
29+
type GoMod struct {
30+
input.Input
31+
}
32+
33+
func (s *GoMod) GetInput() (input.Input, error) {
34+
if s.Path == "" {
35+
s.Path = GoModFile
36+
}
37+
s.TemplateBody = goModTmpl
38+
return s.Input, nil
39+
}
40+
41+
const goModTmpl = `module {{ .Repo }}
42+
43+
require (
44+
cloud.google.com/go v0.30.0 // indirect
45+
github.com/coreos/prometheus-operator v0.26.0 // indirect
46+
github.com/emicklei/go-restful v2.8.1+incompatible // indirect
47+
github.com/ghodss/yaml v1.0.0 // indirect
48+
github.com/go-logr/logr v0.1.0 // indirect
49+
github.com/go-logr/zapr v0.1.0 // indirect
50+
github.com/go-openapi/spec v0.18.0 // indirect
51+
github.com/gogo/protobuf v1.1.1 // indirect
52+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
53+
github.com/golang/groupcache v0.0.0-20180924190550-6f2cf27854a4 // indirect
54+
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
55+
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
56+
github.com/google/uuid v1.0.0 // indirect
57+
github.com/googleapis/gnostic v0.2.0 // indirect
58+
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
59+
github.com/hashicorp/golang-lru v0.5.0 // indirect
60+
github.com/imdario/mergo v0.3.6 // indirect
61+
github.com/json-iterator/go v1.1.5 // indirect
62+
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
63+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
64+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
65+
github.com/onsi/ginkgo v1.7.0 // indirect
66+
github.com/onsi/gomega v1.4.3 // indirect
67+
github.com/operator-framework/operator-sdk v0.4.1-0.20190125185245-3d91991d7b0a
68+
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect
69+
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
70+
github.com/pkg/errors v0.8.0 // indirect
71+
github.com/prometheus/client_golang v0.9.2 // indirect
72+
github.com/spf13/pflag v1.0.3 // indirect
73+
go.uber.org/atomic v1.3.2 // indirect
74+
go.uber.org/multierr v1.1.0 // indirect
75+
go.uber.org/zap v1.9.1 // indirect
76+
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e // indirect
77+
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4 // indirect
78+
golang.org/x/sys v0.0.0-20181019160139-8e24a49d80f8 // indirect
79+
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
80+
golang.org/x/tools v0.0.0-20190124215303-cc6a436ffe6b // indirect
81+
google.golang.org/appengine v1.2.0 // indirect
82+
gopkg.in/inf.v0 v0.9.1 // indirect
83+
k8s.io/api v0.0.0-20181126151915-b503174bad59 // indirect
84+
k8s.io/apiextensions-apiserver v0.0.0-20181126155829-0cd23ebeb688 // indirect
85+
k8s.io/apimachinery v0.0.0-20181126123746-eddba98df674
86+
k8s.io/client-go v2.0.0-alpha.0.0.20181126152608-d082d5923d3c+incompatible
87+
k8s.io/code-generator v0.0.0-20180823001027-3dcf91f64f63
88+
k8s.io/gengo v0.0.0-20181113154421-fd15ee9cc2f7 // indirect
89+
k8s.io/klog v0.1.0 // indirect
90+
k8s.io/kube-openapi v0.0.0-20180711000925-0cf8f7e6ed1d // indirect
91+
sigs.k8s.io/controller-runtime v0.1.8
92+
sigs.k8s.io/testing_frameworks v0.1.0 // indirect
93+
)
94+
`
95+
96+
func ReadDepsFile() ([]byte, error) {
97+
projutil.MustInProjectRoot()
98+
repo := projutil.CheckAndGetProjectGoPkg()
99+
t, err := template.New("").Parse(goModTmpl)
100+
if err != nil {
101+
return nil, fmt.Errorf("failed to parse go mod template: (%v)", err)
102+
}
103+
buf := &bytes.Buffer{}
104+
if err := t.Execute(buf, struct{ Repo string }{Repo: repo}); err != nil {
105+
return nil, fmt.Errorf("failed to execute go mod template: (%v)", err)
106+
}
107+
return buf.Bytes(), nil
108+
}

Diff for: pkg/scaffold/ansible/gopkgtoml.go

-56
This file was deleted.

Diff for: pkg/scaffold/constants.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,21 @@
1414

1515
package scaffold
1616

17-
import (
18-
"path/filepath"
19-
)
17+
import "github.com/operator-framework/operator-sdk/internal/util/fileutil"
2018

2119
const (
22-
// Separator to statically create directories.
23-
filePathSep = string(filepath.Separator)
24-
2520
// dirs
2621
CmdDir = "cmd"
27-
ManagerDir = CmdDir + filePathSep + "manager"
22+
ManagerDir = CmdDir + fileutil.PathSep + "manager"
2823
PkgDir = "pkg"
29-
ApisDir = PkgDir + filePathSep + "apis"
30-
ControllerDir = PkgDir + filePathSep + "controller"
24+
ApisDir = PkgDir + fileutil.PathSep + "apis"
25+
ControllerDir = PkgDir + fileutil.PathSep + "controller"
3126
BuildDir = "build"
32-
BuildTestDir = BuildDir + filePathSep + "test-framework"
33-
BuildBinDir = BuildDir + filePathSep + "_output" + filePathSep + "bin"
34-
BuildScriptDir = BuildDir + filePathSep + "bin"
27+
BuildTestDir = BuildDir + fileutil.PathSep + "test-framework"
28+
BuildBinDir = BuildDir + fileutil.PathSep + "_output" + fileutil.PathSep + "bin"
29+
BuildScriptDir = BuildDir + fileutil.PathSep + "bin"
3530
DeployDir = "deploy"
36-
OLMCatalogDir = DeployDir + filePathSep + "olm-catalog"
37-
CrdsDir = DeployDir + filePathSep + "crds"
31+
OLMCatalogDir = DeployDir + fileutil.PathSep + "olm-catalog"
32+
CrdsDir = DeployDir + fileutil.PathSep + "crds"
3833
VersionDir = "version"
3934
)

0 commit comments

Comments
 (0)