-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds migrate
and run ansible
commands
#887
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
784bd74
adds `migrate` and `run ansible` commands
mhrivnak 0e9a69c
addresses review comments
mhrivnak 1d004aa
Apply suggestions from code review
estroz c845851
Additional improvements based on review.
mhrivnak 96b06a2
update link in docs
mhrivnak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2019 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/operator-framework/operator-sdk/internal/util/projutil" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold/input" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewMigrateCmd returns a command that will add source code to an existing non-go operator | ||
func NewMigrateCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Adds source code to an operator", | ||
Long: `operator-sdk migrate adds a main.go source file and any associated source files for an operator that is not of the "go" type.`, | ||
Run: migrateRun, | ||
} | ||
} | ||
|
||
// migrateRun determines the current operator type and runs the corresponding | ||
// migrate function. | ||
func migrateRun(cmd *cobra.Command, args []string) { | ||
projutil.MustInProjectRoot() | ||
|
||
_ = projutil.CheckAndGetProjectGoPkg() | ||
|
||
opType := projutil.GetOperatorType() | ||
switch opType { | ||
case projutil.OperatorTypeAnsible: | ||
migrateAnsible() | ||
default: | ||
log.Fatalf("operator of type %s cannot be migrated.", opType) | ||
} | ||
} | ||
|
||
// migrateAnsible runs the migration process for an ansible-based operator | ||
func migrateAnsible() { | ||
wd := projutil.MustGetwd() | ||
|
||
cfg := &input.Config{ | ||
AbsProjectPath: wd, | ||
ProjectName: filepath.Base(wd), | ||
} | ||
|
||
dockerfile := ansible.DockerfileHybrid{ | ||
Watches: true, | ||
Roles: true, | ||
} | ||
_, err := os.Stat("playbook.yaml") | ||
switch { | ||
case err == nil: | ||
dockerfile.Playbook = true | ||
case os.IsNotExist(err): | ||
log.Print("No playbook was found, so not including it in the new Dockerfile") | ||
default: | ||
log.Fatalf("error trying to stat playbook.yaml: (%v)", err) | ||
} | ||
|
||
dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile) | ||
newDockerfilePath := dockerfilePath + ".sdkold" | ||
err = os.Rename(dockerfilePath, newDockerfilePath) | ||
if err != nil { | ||
log.Fatalf("failed to rename Dockerfile: (%v)", err) | ||
} | ||
log.Printf("renamed Dockerfile to %s and replaced with newer version", newDockerfilePath) | ||
log.Print("Compare the new Dockerfile to your old one and manually migrate any customizations") | ||
|
||
s := &scaffold.Scaffold{} | ||
err = s.Execute(cfg, | ||
&ansible.Main{}, | ||
&ansible.GopkgToml{}, | ||
&dockerfile, | ||
&ansible.Entrypoint{}, | ||
&ansible.UserSetup{}, | ||
) | ||
if err != nil { | ||
log.Fatalf("add scaffold failed: (%v)", err) | ||
} | ||
} |
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,33 @@ | ||
// Copyright 2019 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/run" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewRunCmd returns a command that contains subcommands to run specific | ||
// operator types. | ||
func NewRunCmd() *cobra.Command { | ||
runCmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Runs a generic operator", | ||
} | ||
|
||
runCmd.AddCommand(run.NewAnsibleCmd()) | ||
return runCmd | ||
} |
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,38 @@ | ||
// Copyright 2019 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package run | ||
|
||
import ( | ||
"github.com/operator-framework/operator-sdk/pkg/ansible" | ||
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var flags *aoflags.AnsibleOperatorFlags | ||
|
||
// NewAnsibleCmd returns a command that will run an ansible operator | ||
func NewAnsibleCmd() *cobra.Command { | ||
newCmd := &cobra.Command{ | ||
Use: "ansible", | ||
Short: "Runs as an ansible operator", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ansible.Run(flags) | ||
estroz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
} | ||
flags = aoflags.AddTo(newCmd.Flags()) | ||
|
||
return newCmd | ||
} |
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
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,45 @@ | ||
// Copyright 2019 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/operator-framework/operator-sdk/internal/util/projutil" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible" | ||
"github.com/operator-framework/operator-sdk/pkg/scaffold/input" | ||
) | ||
|
||
// main renders scaffolds that are required to build the ansible operator base | ||
// image. It is intended for release engineering use only. After running this, | ||
// you can place a binary in `build/_output/bin/ansible-operator` and then run | ||
// `operator-sdk build`. | ||
func main() { | ||
cfg := &input.Config{ | ||
AbsProjectPath: projutil.MustGetwd(), | ||
ProjectName: "ansible-operator", | ||
} | ||
|
||
s := &scaffold.Scaffold{} | ||
err := s.Execute(cfg, | ||
&ansible.DockerfileHybrid{}, | ||
&ansible.Entrypoint{}, | ||
&ansible.UserSetup{}, | ||
) | ||
if err != nil { | ||
log.Fatalf("add scaffold failed: (%v)", err) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonder if this should be a flag or at least alert the user that we are assuming no playbook if this is not found. I could see cases, where playbooks are in a new directory and users, have changed things. Telling the users what we are doing, and what they can do to change it back if that is incorrect might be a good idea. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah a good example of this would be the etcd operator which puts all of it's ansible code under a folder
ansible
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. If they put it in a different location that what we expect, they'll have to manually add it to their new Dockerfile. Presumably they'd catch that in comparing their old Dockerfile to new, but it's worth calling out explicitly.