-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from helen-frank/helen-frank/dev-general
feat: implement instance provider
- Loading branch information
Showing
8 changed files
with
323 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,75 @@ | ||
/* | ||
Copyright 2024 The CloudPilot AI Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package options | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options" | ||
"sigs.k8s.io/karpenter/pkg/utils/env" | ||
) | ||
|
||
func init() { | ||
coreoptions.Injectables = append(coreoptions.Injectables, &Options{}) | ||
} | ||
|
||
type optionsKey struct{} | ||
|
||
type Options struct { | ||
ClusterCABundle string | ||
ClusterName string | ||
ClusterEndpoint string | ||
} | ||
|
||
func (o *Options) AddFlags(fs *coreoptions.FlagSet) { | ||
fs.StringVar(&o.ClusterCABundle, "cluster-ca-bundle", env.WithDefaultString("CLUSTER_CA_BUNDLE", ""), "Cluster CA bundle for nodes to use for TLS connections with the API server. If not set, this is taken from the controller's TLS configuration.") | ||
fs.StringVar(&o.ClusterName, "cluster-name", env.WithDefaultString("CLUSTER_NAME", ""), "[REQUIRED] The kubernetes cluster name for resource discovery.") | ||
fs.StringVar(&o.ClusterEndpoint, "cluster-endpoint", env.WithDefaultString("CLUSTER_ENDPOINT", ""), "The external kubernetes cluster endpoint for new nodes to connect with. If not specified, will discover the cluster endpoint using DescribeCluster API.") | ||
} | ||
|
||
func (o *Options) Parse(fs *coreoptions.FlagSet, args ...string) error { | ||
if err := fs.Parse(args); err != nil { | ||
if errors.Is(err, flag.ErrHelp) { | ||
os.Exit(0) | ||
} | ||
return fmt.Errorf("parsing flags, %w", err) | ||
} | ||
if err := o.Validate(); err != nil { | ||
return fmt.Errorf("validating options, %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Options) ToContext(ctx context.Context) context.Context { | ||
return ToContext(ctx, o) | ||
} | ||
|
||
func ToContext(ctx context.Context, opts *Options) context.Context { | ||
return context.WithValue(ctx, optionsKey{}, opts) | ||
} | ||
|
||
func FromContext(ctx context.Context) *Options { | ||
retval := ctx.Value(optionsKey{}) | ||
if retval == nil { | ||
return nil | ||
} | ||
return retval.(*Options) | ||
} |
This file contains 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,51 @@ | ||
/* | ||
Copyright 2024 The CloudPilot AI Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package options | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"go.uber.org/multierr" | ||
) | ||
|
||
func (o Options) Validate() error { | ||
return multierr.Combine( | ||
o.validateEndpoint(), | ||
o.validateRequiredFields(), | ||
) | ||
} | ||
|
||
func (o Options) validateEndpoint() error { | ||
if o.ClusterEndpoint == "" { | ||
return nil | ||
} | ||
endpoint, err := url.Parse(o.ClusterEndpoint) | ||
// url.Parse() will accept a lot of input without error; make | ||
// sure it's a real URL | ||
if err != nil || !endpoint.IsAbs() || endpoint.Hostname() == "" { | ||
return fmt.Errorf("%q is not a valid cluster-endpoint URL", o.ClusterEndpoint) | ||
} | ||
return nil | ||
} | ||
|
||
func (o Options) validateRequiredFields() error { | ||
if o.ClusterName == "" { | ||
return fmt.Errorf("missing field, cluster-name") | ||
} | ||
return nil | ||
} |
This file contains 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 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
Oops, something went wrong.