-
Notifications
You must be signed in to change notification settings - Fork 1.5k
AGENT-853: Add ClusterInfo asset #7997
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
Merged
openshift-merge-bot
merged 3 commits into
openshift:master
from
andfasano:agent-day2-clusterinfo-asset
Mar 1, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,80 @@ | ||
| package joiner | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/openshift/installer/pkg/asset" | ||
| ) | ||
|
|
||
| const ( | ||
| addNodesParamsFile = ".addnodesparams" | ||
| ) | ||
|
|
||
| // AddNodesConfig is used to store the current configuration | ||
| // for the command. | ||
| type AddNodesConfig struct { | ||
| File *asset.File | ||
| Params Params | ||
| } | ||
|
|
||
| // Params is used to store the command line parameters. | ||
| type Params struct { | ||
| Kubeconfig string `json:"kubeconfig,omitempty"` | ||
| } | ||
|
|
||
| // Save stores the current parameters on disk. | ||
| func (p *Params) Save(assetsDir string) error { | ||
| data, err := json.Marshal(p) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fileName := filepath.Join(assetsDir, addNodesParamsFile) | ||
| return os.WriteFile(fileName, data, 0o600) | ||
| } | ||
|
|
||
| // Name returns the human-friendly name of the asset. | ||
| func (*AddNodesConfig) Name() string { | ||
| return "AddNodes Config" | ||
| } | ||
|
|
||
| // Dependencies returns all of the dependencies directly needed to generate | ||
| // the asset. | ||
| func (*AddNodesConfig) Dependencies() []asset.Asset { | ||
| return []asset.Asset{} | ||
| } | ||
|
|
||
| // Generate it's empty for this asset, always loaded from disk. | ||
| func (*AddNodesConfig) Generate(dependencies asset.Parents) error { | ||
| return nil | ||
| } | ||
|
|
||
| // Files returns the files generated by the asset. | ||
| func (a *AddNodesConfig) Files() []*asset.File { | ||
| if a.File != nil { | ||
| return []*asset.File{a.File} | ||
| } | ||
| return []*asset.File{} | ||
| } | ||
|
|
||
| // Load returns agent config asset from the disk. | ||
| func (a *AddNodesConfig) Load(f asset.FileFetcher) (bool, error) { | ||
| file, err := f.FetchByName(addNodesParamsFile) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return false, nil | ||
| } | ||
| return false, fmt.Errorf("failed to load %s file: %w", addNodesParamsFile, err) | ||
| } | ||
|
|
||
| params := &Params{} | ||
| if err := json.Unmarshal(file.Data, params); err != nil { | ||
| return false, fmt.Errorf("failed to unmarshal %s: %w", addNodesParamsFile, err) | ||
| } | ||
|
|
||
| a.Params = *params | ||
| return true, nil | ||
| } | ||
This file contains hidden or 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,135 @@ | ||
| package joiner | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/url" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/client-go/kubernetes" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
|
|
||
| configclient "github.com/openshift/client-go/config/clientset/versioned" | ||
| "github.com/openshift/installer/pkg/asset" | ||
| "github.com/openshift/installer/pkg/asset/agent/workflow" | ||
| ) | ||
|
|
||
| // ClusterInfo it's an asset used to retrieve config info | ||
| // from an already existing cluster. | ||
| type ClusterInfo struct { | ||
| ClusterID string | ||
| APIDNSName string | ||
| PullSecret string | ||
| } | ||
|
|
||
| var _ asset.WritableAsset = (*ClusterInfo)(nil) | ||
|
|
||
| // Name returns the human-friendly name of the asset. | ||
| func (ci *ClusterInfo) Name() string { | ||
| return "Agent Installer ClusterInfo" | ||
| } | ||
|
|
||
| // Dependencies returns all of the dependencies directly needed to generate | ||
| // the asset. | ||
| func (*ClusterInfo) Dependencies() []asset.Asset { | ||
| return []asset.Asset{ | ||
| &workflow.AgentWorkflow{}, | ||
| &AddNodesConfig{}, | ||
| } | ||
| } | ||
|
|
||
| // Generate generates the ClusterInfo. | ||
| func (ci *ClusterInfo) Generate(dependencies asset.Parents) error { | ||
| agentWorkflow := &workflow.AgentWorkflow{} | ||
| addNodesConfig := &AddNodesConfig{} | ||
| dependencies.Get(agentWorkflow, addNodesConfig) | ||
|
|
||
| if agentWorkflow.Workflow != workflow.AgentWorkflowTypeAddNodes { | ||
| return nil | ||
| } | ||
|
|
||
| config, err := ci.getRestConfig(addNodesConfig.Params.Kubeconfig) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = ci.retrieveClusterID(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = ci.retrieveAPIDNSName(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = ci.retrievePullSecret(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (ci *ClusterInfo) getRestConfig(kubeconfig string) (*rest.Config, error) { | ||
| var err error | ||
| var config *rest.Config | ||
|
|
||
| if kubeconfig != "" { | ||
| config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
| } else { | ||
| config, err = rest.InClusterConfig() | ||
| } | ||
|
|
||
| return config, err | ||
| } | ||
|
|
||
| func (ci *ClusterInfo) retrieveClusterID(config *rest.Config) error { | ||
| clientset, err := configclient.NewForConfig(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cv, err := clientset.ConfigV1().ClusterVersions().Get(context.Background(), "version", metav1.GetOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| ci.ClusterID = string(cv.Spec.ClusterID) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (ci *ClusterInfo) retrieveAPIDNSName(config *rest.Config) error { | ||
| parsedURL, err := url.Parse(config.Host) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ci.APIDNSName = parsedURL.Hostname() | ||
| return nil | ||
| } | ||
|
|
||
| func (ci *ClusterInfo) retrievePullSecret(config *rest.Config) error { | ||
| clientset, err := kubernetes.NewForConfig(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| pullSecret, err := clientset.CoreV1().Secrets("openshift-config").Get(context.Background(), "pull-secret", metav1.GetOptions{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| ci.PullSecret = string(pullSecret.Data[".dockerconfigjson"]) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Files returns the files generated by the asset. | ||
| func (*ClusterInfo) Files() []*asset.File { | ||
| return []*asset.File{} | ||
| } | ||
|
|
||
| // Load returns agent config asset from the disk. | ||
| func (*ClusterInfo) Load(f asset.FileFetcher) (bool, error) { | ||
| return false, nil | ||
| } |
This file contains hidden or 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,65 @@ | ||
| package workflow | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/openshift/installer/pkg/asset" | ||
| ) | ||
|
|
||
| // AgentWorkflow allows other assets to check | ||
| // which is the workflow currently active. | ||
| type AgentWorkflow struct { | ||
| File *asset.File | ||
| Workflow AgentWorkflowType | ||
| } | ||
|
|
||
| var _ asset.WritableAsset = (*AgentWorkflow)(nil) | ||
|
|
||
| // Name returns a human friendly name for the asset. | ||
| func (*AgentWorkflow) Name() string { | ||
| return "Agent Workflow" | ||
| } | ||
|
|
||
| // Dependencies returns all of the dependencies directly needed to generate | ||
| // the asset. | ||
| func (*AgentWorkflow) Dependencies() []asset.Asset { | ||
| return []asset.Asset{} | ||
| } | ||
|
|
||
| // Generate generates the AgentWorkflow asset. | ||
| func (a *AgentWorkflow) Generate(dependencies asset.Parents) error { | ||
| // Set install workflow as a default | ||
| a.Workflow = AgentWorkflowTypeInstall | ||
| a.File = &asset.File{ | ||
| Filename: agentWorkflowFilename, | ||
| Data: []byte(a.Workflow), | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Files returns the files generated by the asset. | ||
| func (a *AgentWorkflow) Files() []*asset.File { | ||
| if a.File != nil { | ||
| return []*asset.File{a.File} | ||
| } | ||
| return []*asset.File{} | ||
| } | ||
|
|
||
| // Load returns the asset from disk. | ||
| func (a *AgentWorkflow) Load(f asset.FileFetcher) (bool, error) { | ||
| file, err := f.FetchByName(agentWorkflowFilename) | ||
| if err != nil { | ||
| if os.IsNotExist(err) { | ||
| return false, nil | ||
| } | ||
| return false, fmt.Errorf("failed to load %s file: %w", agentWorkflowFilename, err) | ||
| } | ||
|
|
||
| // Get the current workflow | ||
| a.Workflow = AgentWorkflowType(file.Data) | ||
| a.File = file | ||
|
|
||
| return true, nil | ||
| } |
This file contains hidden or 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,27 @@ | ||
| package workflow | ||
|
|
||
| import "github.com/openshift/installer/pkg/asset" | ||
|
|
||
| // AgentWorkflowAddNodes is meant just to define | ||
| // the add nodes workflow. | ||
| type AgentWorkflowAddNodes struct { | ||
| AgentWorkflow | ||
| } | ||
|
|
||
| var _ asset.WritableAsset = (*AgentWorkflowAddNodes)(nil) | ||
|
|
||
| // Name returns a human friendly name for the asset. | ||
| func (*AgentWorkflowAddNodes) Name() string { | ||
| return "Agent Workflow Add Nodes" | ||
| } | ||
|
|
||
| // Generate generates the AgentWorkflow asset. | ||
| func (a *AgentWorkflowAddNodes) Generate(dependencies asset.Parents) error { | ||
| a.Workflow = AgentWorkflowTypeAddNodes | ||
| a.File = &asset.File{ | ||
| Filename: agentWorkflowFilename, | ||
| Data: []byte(a.Workflow), | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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,14 @@ | ||
| package workflow | ||
|
|
||
| // AgentWorkflowType defines the supported | ||
| // agent workflows. | ||
| type AgentWorkflowType string | ||
|
|
||
| const ( | ||
| // AgentWorkflowTypeInstall identifies the install workflow. | ||
| AgentWorkflowTypeInstall AgentWorkflowType = "install" | ||
| // AgentWorkflowTypeAddNodes identifies the add nodes workflow. | ||
| AgentWorkflowTypeAddNodes AgentWorkflowType = "addnodes" | ||
|
|
||
| agentWorkflowFilename = ".agentworkflow" | ||
| ) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.