-
Notifications
You must be signed in to change notification settings - Fork 40
Adds --distribution flag, refactor k8s generation #250
Conversation
This refactors the |
pkg/cmd/kubernetes.go
Outdated
func ExecuteKubectl(paths []string, args ...string) error { | ||
// Generate Kubernetes Artifacts and either writes to file | ||
// or uses kubectl to deploy. | ||
func CreateKubernetesArtifacts(paths []string, generate bool, args ...string) error { |
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.
@cdrage I like the idea, but now this function is doing too many things. How about we create a new function which either writes to a file or deploys to cluster and let this one be?
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.
That's how it was before and both functions used / copied the same code hence why I've gone ahead and made it into one function that either writes or executes / deploy.
I agree it may be pushing it in regards to doing too many things, but before it was to similar functions in two separate files.
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.
What I meant was that, in the current master, the following part is common for both Generate()
and ExecuteKubectl()
functions, so we can abstract that out in a single function and reuse it in all the cmd/* files. And the remaining uncommon part can go into separate functions, one with writeObject
part, another with runKubectl
part.
So -
- Function 1 with common part
files, err := GetAllYAMLFiles(paths)
if err != nil {
return errors.Wrap(err, "unable to get YAML files")
}
inputs, err := getApplicationsFromFiles(files)
if err != nil {
return errors.Wrap(err, "unable to get kedge definitions from input files")
}
for _, input := range inputs {
ros, extraResources, err := spec.CoreOperations(input.data)
if err != nil {
return errors.Wrap(err, "unable to perform controller operations")
}
// write all the kubernetes objects that were generated
for _, runtimeObject := range ros {
data, err := yaml.Marshal(runtimeObject)
if err != nil {
return errors.Wrap(err, "failed to marshal object")
}
- Function 2 with
writeObject
part
err = writeObject(data)
if err != nil {
return errors.Wrap(err, "failed to write object")
}
}
for _, file := range extraResources {
// change the file name to absolute file name
// then read the file and then pass it to writeObject
file = findAbsPath(input.fileName, file)
data, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrap(err, "file reading failed")
}
err = writeObject(data)
if err != nil {
return errors.Wrap(err, "failed to write object")
}
}
}
- Function 3 with
runKubectl
part
arguments := append(args, "-f", "-")
err = runKubectl(arguments, data)
if err != nil {
return errors.Wrap(err, "kubectl error")
}
}
for _, file := range extraResources {
// change the file name to absolute file name
file = findAbsPath(input.fileName, file)
// We need to add "-f absolute-filename" at the end of the command passed to us to
// pass the generated files.
// e.g. If the command and arguments are "apply --namespace staging", then the
// final command becomes "kubectl apply --namespace staging -f absolute-filename"
arguments := append(args, "-f", file)
err = runKubectl(arguments, nil)
if err != nil {
return errors.Wrap(err, "kubectl error")
}
}
So instead of a bigger one, we break it down to smaller 3 more testable functions.
WDYT?
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.
@containscafeine I initially separated it into three different functions but found that I was over-engineering it. For example, I put this into one function:
files, err := GetAllYAMLFiles(paths)
if err != nil {
return errors.Wrap(err, "unable to get YAML files")
}
inputs, err := getApplicationsFromFiles(files)
if err != nil {
return errors.Wrap(err, "unable to get kedge definitions from input files")
}
Which was literally a function calling two other functions and erroring it out.. Only reducing the amount of lines by 4.
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.
I think it would be better to have two functions, one that generates Kubernetes artifacts (returns string that is complete representation of kedge file including extra resources or error). And other that takes that string and calls kubectl.
Having one big function with a boolean argument that switches what that function is doing is a bit untransparent.
There are two different functionalities in that function hidden behind one boolean switch.
I would do it differently, but I'm OK with how it is done in this PR.
edd5a07
to
dbf1e28
Compare
Turning into a larger PR, but I've gone ahead and added some small tests too. |
dbf1e28
to
08434f5
Compare
604ed46
to
514397b
Compare
Hey all, since #237 needs to be implemented too, I'm going to change this back to a WIP, update the commit message as well as add the "deploymentconfig" option / controller. |
646b4ea
to
395ab50
Compare
6357b49
to
5e22a43
Compare
Review time 👍 |
32a08ef
to
ccae982
Compare
@containscafeine Do you mind if if we continue with this PR and then we can refactor it later, in order for me to open up and continue with #257 and your jobs PR #116 ? Reason for combing is to get rid of I've gone ahead and added a note to the code as well as TODO 👍 |
This commit adds the --distribution flag as well as refactors the generate.go file to one single file, kubernetes.go. This allows for all future changes to simply be made within kubernetes.go rather than two separate files with similar code (generate.go + kubernetes.go)
ccae982
to
8c2c9aa
Compare
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.
Yay, pragmatism FTW! Let's do the refactor later.
This commit adds the --distribution flag as well as refactors the
generate.go file to one single file, kubernetes.go
This allows for all future changes to simply be made within
kubernetes.go rather than two separate files with similar code
(generate.go + kubernetes.go)