This is a combination of 4 commits.
Fix the image
This commit is contained in:
@ -4,22 +4,30 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
rbacv1 "k8s.io/api/rbac/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
Controller ctrl.Manager
|
||||
Params AccountParams
|
||||
Data *AccountData
|
||||
Kubeconfig string
|
||||
Token string
|
||||
}
|
||||
|
||||
type AccountParams struct {
|
||||
HashCost int16
|
||||
}
|
||||
type AccountData struct {
|
||||
Username string
|
||||
Password string
|
||||
@ -28,7 +36,7 @@ type AccountData struct {
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 1)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
@ -37,21 +45,47 @@ func CheckPasswordHash(password, hash string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func waitUntilCreated(ctx context.Context, client client.Client ,obj client.Object, attemps int, timeout time.Duration) error {
|
||||
log.Printf("Waiting %d", attemps)
|
||||
if err := client.Get(ctx, types.NamespacedName{
|
||||
Namespace: obj.GetNamespace(),
|
||||
Name: obj.GetName(),
|
||||
}, obj); err != nil {
|
||||
if attemps > 0 {
|
||||
time.Sleep(timeout)
|
||||
waitUntilCreated(ctx, client, obj, attemps - 1, timeout)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (acc *Account) Create(ctx context.Context) error {
|
||||
client := acc.Controller.GetClient()
|
||||
|
||||
acc.Data.UUID = uuid.New().String()
|
||||
log.Println(acc.Data.UUID)
|
||||
passwordHash, err := HashPassword(acc.Data.Password)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
namespace := corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: acc.Data.UUID,
|
||||
},
|
||||
}
|
||||
|
||||
if err := client.Create(ctx, &namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := waitUntilCreated(ctx, client, &namespace, 10, time.Millisecond * 50); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
if err := client.Get(ctx, types.NamespacedName{
|
||||
Name: acc.Data.UUID,
|
||||
}, &namespace); err != nil {
|
||||
@ -156,8 +190,11 @@ func (acc *Account) Create(ctx context.Context) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err := waitUntilCreated(ctx, client, saSec, 10, time.Millisecond * 50); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
acc.Kubeconfig, err = acc.getToken(ctx, saSec)
|
||||
acc.Token, err = acc.getToken(ctx, saSec)
|
||||
if err != nil {
|
||||
if err := client.Delete(ctx, &namespace); err != nil {
|
||||
return err
|
||||
@ -193,7 +230,7 @@ func (acc *Account) Login (ctx context.Context) error {
|
||||
Type: "kubernetes.io/service-account-token",
|
||||
}
|
||||
var err error
|
||||
acc.Kubeconfig, err = acc.getToken(ctx, saSec)
|
||||
acc.Token, err = acc.getToken(ctx, saSec)
|
||||
if err != nil{
|
||||
return err
|
||||
}
|
||||
@ -210,3 +247,4 @@ func (acc *Account) getToken (ctx context.Context, saSec *corev1.Secret) (string
|
||||
}
|
||||
return string(saSec.Data["token"]), nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user