Skip to content
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

Refactoring: Support of different deployments kind #30

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

dgritsayqm
Copy link
Contributor

Resolves #29

@dgritsayqm dgritsayqm requested a review from amukherj1 August 8, 2019 14:43
@dgritsayqm dgritsayqm self-assigned this Aug 8, 2019
Copy link
Contributor

@amukherj1 amukherj1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes suggested, plus added a couple of questions.

@@ -4,6 +4,19 @@ import (
"time"
)

// DeploymentClient is a common interface for any deployment kinds.
type DeploymentClient interface {
UpgradeCheck(LocalSolutionInfo) bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest that the names of functions be imperative, like DoSomething.

CheckUpgrade
CheckHealth

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I'll do changes.

@@ -0,0 +1,36 @@
package upgradeagent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest file names in snake case.

docker_compose_deployment.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll rename the files.

@@ -0,0 +1,36 @@
package upgradeagent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would suggest file names in snake case.

kubernetes_deployment.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

@@ -4,6 +4,19 @@ import (
"time"
)

// DeploymentClient is a common interface for any deployment kinds.
type DeploymentClient interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whose client is this interface? Which is the server?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client of this interface is the Patrao agent. It's not implemented yet. I'm working on it now. I suppose Today evening or Monday morning I'll commit these changes.

}

// UpstreamClient is a common interface for any upstream service kinds
type UpstreamClient interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whose client is this interface? Which is the server? Who uses this interface / calls its methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client of this interface is DeploymentClient. We pass UpstreamClient while creation of DeploymentClient instance and then created instance calls RequestUpgrade() functions. For example, please see func (d *dockerComposeDeployment) UpgradeCheck(localSolutionInfo LocalSolutionInfo) bool implementation. As for the server (ip:port) it will be received from the command line arguments inside the implementation of mockUpstreamClient. Actually in the same way how it's working now.

@dgritsayqm dgritsayqm requested a review from amukherj1 August 12, 2019 13:31
continue
}
val, exist := containersSpec[serviceName]
if (exist) && (upgradeInfo.Name == solutionName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid the parentheses.

if val, exist := containersSpec[serviceName]; exist && upgradeInfo.Name == solutionName {
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


rc = false
containersSpec := make(map[string]ContainerSpec)
specMap := make(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're unmarshaling into this map, you don't need to to preallocate. More idiomatic:

var specMap map[string]interface{}
if err := yaml.Unmarshal([]byte(upgradeInfo.Spec), &specMap); err != nil {
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the map variable is a reference type like pointer it has null. So we have to preallocate the map before passing it to yaml.Unmarshal

}
if found {
containersSpec[service.(string)] = ContainerSpec{Name: service.(string), Image: serviceImageName}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified:

if val, itemFound := details[DockerComposeImageName]; itemFound {
    if serviceImageName, ok := val.(string); ok {
        containersSpec[service.(string)] = ContainerSpec{Name: service.(string), Image: serviceImageName}
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

return false
}
if val, exists := specMap[DockerComposeServicesName]; exists {
servicesMap := val.(map[interface{}]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the following work? Might be slightly simpler code. Plus we must check for ok:

servicesMap, ok := val.(map[string]interface{})
if !ok {
    continue
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

}
err := os.Mkdir(d.upgradeInfo.Name, os.ModePerm)
if nil != err {
log.Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to add meaningful context info to err before logging and returning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

if err != nil {
log.Error(err)
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err = cmd.Run(); err != nil {
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

return true
}
time.Sleep(1 * time.Second)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reduce indentation be removing these {...}.

@dgritsayqm dgritsayqm requested a review from amukherj1 September 5, 2019 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code refactoring
2 participants