Skip to content

Commit

Permalink
Merge pull request #1898 from littleBlackHouse/auth-type
Browse files Browse the repository at this point in the history
fix: add secret option to auth-type.
  • Loading branch information
ks-ci-bot authored Jul 3, 2023
2 parents d5d2b81 + c7f4c89 commit d620088
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/v1beta1/auth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type Auth struct {
// +optional
PrivateKeyPath string `yaml:"privateKeyPath,omitempty" json:"privateKeyPath,omitempty"`

// Secret is the secret of the PrivateKey or Password for SSH authentication.It should in the same namespace as capkk.
// When Password is empty, replace it with data.password.
// When PrivateKey is empty, replace it with data.privateKey
// +optional
Secret string `yaml:"secret,omitempty" json:"secret,omitempty"`

// Timeout is the timeout for establish an SSH connection.
// +optional
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/kkcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func validateLoadBalancer(loadBalancer *KKLoadBalancerSpec) []*field.Error {
func validateClusterNodes(nodes Nodes) []*field.Error {
var errs field.ErrorList

if nodes.Auth.Password == "" && nodes.Auth.PrivateKey == "" && nodes.Auth.PrivateKeyPath == "" {
if nodes.Auth.Password == "" && nodes.Auth.PrivateKey == "" && nodes.Auth.PrivateKeyPath == "" && nodes.Auth.Secret == "" {
errs = append(errs, field.Required(field.NewPath("spec", "nodes", "auth"), "password and privateKey can't both be empty"))
}

Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_kkclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ spec:
description: PrivateKeyFile is the path to the private key
for SSH authentication.
type: string
secret:
description: Secret is the secret of the PrivateKey or Password
for SSH authentication.It should in the same namespace as
capkk. When Password is empty, replace it with data.password.
When PrivateKey is empty, replace it with data.privateKey
type: string
timeout:
description: Timeout is the timeout for establish an SSH connection.
format: int64
Expand Down Expand Up @@ -193,6 +199,13 @@ spec:
description: PrivateKeyFile is the path to the private
key for SSH authentication.
type: string
secret:
description: Secret is the secret of the PrivateKey
or Password for SSH authentication.It should in the
same namespace as capkk. When Password is empty, replace
it with data.password. When PrivateKey is empty, replace
it with data.privateKey
type: string
timeout:
description: Timeout is the timeout for establish an
SSH connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ spec:
description: PrivateKeyFile is the path to the private
key for SSH authentication.
type: string
secret:
description: Secret is the secret of the PrivateKey
or Password for SSH authentication.It should in
the same namespace as capkk. When Password is empty,
replace it with data.password. When PrivateKey is
empty, replace it with data.privateKey
type: string
timeout:
description: Timeout is the timeout for establish
an SSH connection.
Expand Down Expand Up @@ -218,6 +225,13 @@ spec:
description: PrivateKeyFile is the path to the
private key for SSH authentication.
type: string
secret:
description: Secret is the secret of the PrivateKey
or Password for SSH authentication.It should
in the same namespace as capkk. When Password
is empty, replace it with data.password. When
PrivateKey is empty, replace it with data.privateKey
type: string
timeout:
description: Timeout is the timeout for establish
an SSH connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ spec:
description: PrivateKeyFile is the path to the private key for
SSH authentication.
type: string
secret:
description: Secret is the secret of the PrivateKey or Password
for SSH authentication.It should in the same namespace as capkk.
When Password is empty, replace it with data.password. When
PrivateKey is empty, replace it with data.privateKey
type: string
timeout:
description: Timeout is the timeout for establish an SSH connection.
format: int64
Expand Down
15 changes: 15 additions & 0 deletions controllers/kkinstance/kkinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (

"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -95,6 +97,19 @@ func (r *Reconciler) getSSHClient(scope *scope.InstanceScope) ssh.Interface {
if r.sshClientFactory != nil {
return r.sshClientFactory(scope)
}
if scope.KKInstance.Spec.Auth.Secret != "" {
secret := &corev1.Secret{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
if err := r.Get(ctx, types.NamespacedName{Namespace: scope.Cluster.Namespace, Name: scope.KKInstance.Spec.Auth.Secret}, secret); err == nil {
if scope.KKInstance.Spec.Auth.PrivateKey == "" { // replace PrivateKey by secret
scope.KKInstance.Spec.Auth.PrivateKey = string(secret.Data["privateKey"])
}
if scope.KKInstance.Spec.Auth.Password == "" { // replace password by secret
scope.KKInstance.Spec.Auth.Password = string(secret.Data["password"])
}
}
}
return ssh.NewClient(scope.KKInstance.Spec.Address, scope.KKInstance.Spec.Auth, &scope.Logger)
}

Expand Down

0 comments on commit d620088

Please sign in to comment.