mirror of
https://github.com/openshift/installer.git
synced 2026-02-05 15:47:14 +01:00
added ClusterInfo asset
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
@@ -19,7 +20,11 @@ func main() {
|
||||
Use: "add-nodes",
|
||||
Short: "Generates an ISO that could be used to boot the configured nodes to let them join an existing cluster",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return nodejoiner.NewAddNodesCommand(wd)
|
||||
kubeConfig, err := cmd.Flags().GetString("kubeconfig")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nodejoiner.NewAddNodesCommand(wd, kubeConfig)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -34,6 +39,7 @@ func main() {
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "node-joiner",
|
||||
}
|
||||
rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file.")
|
||||
|
||||
rootCmd.AddCommand(nodesAddCmd)
|
||||
rootCmd.AddCommand(nodesMonitorCmd)
|
||||
|
||||
79
pkg/asset/agent/joiner/addnodesconfig.go
Normal file
79
pkg/asset/agent/joiner/addnodesconfig.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package joiner
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/openshift/installer/pkg/asset"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
addNodesParamsFile = ".addnodesparams"
|
||||
)
|
||||
|
||||
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, 0o644)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
136
pkg/asset/agent/joiner/clusterinfo.go
Normal file
136
pkg/asset/agent/joiner/clusterinfo.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package joiner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
|
||||
configclient "github.com/openshift/client-go/config/clientset/versioned"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
"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
|
||||
}
|
||||
@@ -2,11 +2,24 @@ package nodejoiner
|
||||
|
||||
import (
|
||||
"github.com/openshift/installer/pkg/asset"
|
||||
"github.com/openshift/installer/pkg/asset/agent/joiner"
|
||||
"github.com/openshift/installer/pkg/asset/agent/workflow"
|
||||
"github.com/openshift/installer/pkg/asset/store"
|
||||
)
|
||||
|
||||
// NewAddNodesCommand creates a new command for add nodes.
|
||||
func NewAddNodesCommand(directory string) error {
|
||||
func NewAddNodesCommand(directory string, kubeConfig string) error {
|
||||
|
||||
// Store the current parameters into the assets folder, so
|
||||
// that they could be retrieved later by the assets
|
||||
params := joiner.Params{
|
||||
Kubeconfig: kubeConfig,
|
||||
}
|
||||
params.Save(directory)
|
||||
|
||||
fetcher := store.NewAssetsFetcher(directory)
|
||||
return fetcher.FetchAndPersist([]asset.WritableAsset{})
|
||||
return fetcher.FetchAndPersist([]asset.WritableAsset{
|
||||
&workflow.AgentWorkflowAddNodes{},
|
||||
// To be completed
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user