-
Notifications
You must be signed in to change notification settings - Fork 439
Introduce Agent platform #760
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| apifixtures "github.com/openshift/hypershift/api/fixtures" | ||
| "github.com/openshift/hypershift/cmd/cluster/core" | ||
| "github.com/spf13/cobra" | ||
| utilrand "k8s.io/apimachinery/pkg/util/rand" | ||
| ) | ||
|
|
||
| func NewCreateCommand(opts *core.CreateOptions) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "agent", | ||
| Short: "Creates basic functional HostedCluster resources on Agent", | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| opts.AgentPlatform = core.AgentPlatformCreateOptions{} | ||
|
|
||
| cmd.Run = func(cmd *cobra.Command, args []string) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| sigs := make(chan os.Signal, 1) | ||
| signal.Notify(sigs, syscall.SIGINT) | ||
| go func() { | ||
| <-sigs | ||
| cancel() | ||
| }() | ||
|
|
||
| if err := CreateCluster(ctx, opts); err != nil { | ||
| log.Error(err, "Failed to create cluster") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func CreateCluster(ctx context.Context, opts *core.CreateOptions) error { | ||
| return core.CreateCluster(ctx, opts, applyPlatformSpecificsValues) | ||
| } | ||
|
|
||
| func applyPlatformSpecificsValues(ctx context.Context, exampleOptions *apifixtures.ExampleOptions, opts *core.CreateOptions) (err error) { | ||
| if opts.AgentPlatform.APIServerAddress == "" { | ||
| opts.AgentPlatform.APIServerAddress, err = core.GetAPIServerAddressByNode(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| infraID := opts.InfraID | ||
| if len(infraID) == 0 { | ||
| infraID = fmt.Sprintf("%s-%s", opts.Name, utilrand.String(5)) | ||
| } | ||
| exampleOptions.InfraID = infraID | ||
|
|
||
| // TODO: We are inconsistent atm as DNS basedomain input is required and platform agnostic at the HostedCluster API level. | ||
| // However In the CLI input is only exposed in AWS and not required atm. This should either be core input and required for | ||
| // every platform or the field in the API should be optional if there's platform which don't need it. | ||
| exampleOptions.BaseDomain = "example.com" | ||
avishayt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| exampleOptions.Agent = &apifixtures.ExampleAgentOptions{ | ||
| APIServerAddress: opts.AgentPlatform.APIServerAddress, | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/openshift/hypershift/cmd/cluster/core" | ||
| "github.com/openshift/hypershift/cmd/cluster/none" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type DestroyOptions struct { | ||
| Namespace string | ||
| Name string | ||
| ClusterGracePeriod time.Duration | ||
| } | ||
|
|
||
| func NewDestroyCommand(opts *core.DestroyOptions) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "agent", | ||
| Short: "Destroys a HostedCluster and its associated infrastructure on Agent.", | ||
| SilenceUsage: true, | ||
| } | ||
|
|
||
| cmd.Run = func(cmd *cobra.Command, args []string) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| sigs := make(chan os.Signal, 1) | ||
| signal.Notify(sigs, syscall.SIGINT) | ||
| go func() { | ||
| <-sigs | ||
| cancel() | ||
| }() | ||
|
|
||
| if err := DestroyCluster(ctx, opts); err != nil { | ||
| log.Error(err, "Failed to destroy cluster") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func DestroyCluster(ctx context.Context, o *core.DestroyOptions) error { | ||
| return none.DestroyCluster(ctx, o) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "github.com/openshift/hypershift/cmd/util" | ||
| ) | ||
|
|
||
| var log = util.Log |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ import ( | |
| "github.com/openshift/hypershift/cmd/version" | ||
| hyperapi "github.com/openshift/hypershift/support/api" | ||
| "golang.org/x/crypto/ssh" | ||
| corev1 "k8s.io/api/core/v1" | ||
| v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| kubeclient "k8s.io/client-go/kubernetes" | ||
| crclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
|
|
@@ -44,6 +47,11 @@ type CreateOptions struct { | |
| SSHKeyFile string | ||
| NonePlatform NonePlatformCreateOptions | ||
| AWSPlatform AWSPlatformOptions | ||
| AgentPlatform AgentPlatformCreateOptions | ||
| } | ||
|
|
||
| type AgentPlatformCreateOptions struct { | ||
| APIServerAddress string | ||
| } | ||
|
|
||
| type NonePlatformCreateOptions struct { | ||
|
|
@@ -178,6 +186,39 @@ func apply(ctx context.Context, exampleOptions *apifixtures.ExampleOptions, rend | |
| return nil | ||
| } | ||
|
|
||
| func GetAPIServerAddressByNode(ctx context.Context) (string, error) { | ||
| // Fetch a single node and determine possible DNS or IP entries to use | ||
| // for external node-port communication. | ||
| // Possible values are considered with the following priority based on the address type: | ||
| // - NodeExternalDNS | ||
| // - NodeExternalIP | ||
| // - NodeInternalIP | ||
| apiServerAddress := "" | ||
| kubeClient := kubeclient.NewForConfigOrDie(util.GetConfigOrDie()) | ||
| nodes, err := kubeClient.CoreV1().Nodes().List(ctx, v1.ListOptions{Limit: 1}) | ||
| if err != nil { | ||
| return "", fmt.Errorf("unable to fetch node objects: %w", err) | ||
| } | ||
| if len(nodes.Items) < 1 { | ||
| return "", fmt.Errorf("no node objects found: %w", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was moved from the None platform as-is for reuse by the Agent platform, I'd rather not change it in this PR
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why though? would .List though an error if nodes.Items is of len 0? If so +1 to address separately and keep this PR focus on the multi-platform structure. |
||
| } | ||
| addresses := map[corev1.NodeAddressType]string{} | ||
| for _, address := range nodes.Items[0].Status.Addresses { | ||
| addresses[address.Type] = address.Address | ||
| } | ||
| for _, addrType := range []corev1.NodeAddressType{corev1.NodeExternalDNS, corev1.NodeExternalIP, corev1.NodeInternalIP} { | ||
| if address, exists := addresses[addrType]; exists { | ||
| apiServerAddress = address | ||
| break | ||
| } | ||
| } | ||
| if apiServerAddress == "" { | ||
| return "", fmt.Errorf("node %q does not expose any IP addresses, this should not be possible", nodes.Items[0].Name) | ||
| } | ||
| log.Info(fmt.Sprintf("detected %q from node %q as external-api-server-address", apiServerAddress, nodes.Items[0].Name)) | ||
| return apiServerAddress, nil | ||
| } | ||
|
|
||
| func CreateCluster(ctx context.Context, opts *CreateOptions, platformSpecificApply ApplyPlatformSpecifics) error { | ||
| exampleOptions, err := createCommonFixture(opts) | ||
| if err != nil { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.