Skip to content

Commit

Permalink
Proposal for controlling minio via CRD in a declarative way
Browse files Browse the repository at this point in the history
  • Loading branch information
cniackz committed Oct 27, 2023
1 parent d7119ec commit 8eb0d3d
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ nancy
examples/.DS_Store
testing/openshift/bundle/*
examples/**/obj/
.idea
.idea
operator.iml
go_build_github_com_minio_operator_cmd_operator
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ require (
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
Expand Down Expand Up @@ -474,6 +476,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/gopherjs/gopherjs v0.0.0-20220104163920-15ed2e8cf2bd/go.mod h1:cz9oNYuRUWGdHmLF2IodMLkAhcPtXeULvcBNagUrxTI=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
Expand Down Expand Up @@ -658,6 +661,8 @@ github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
9 changes: 9 additions & 0 deletions manifests/minio.min.io_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,13 @@ spec:
type: string
type: object
type: array
ldapPolicyAttachToSingleUser:
type: object
properties:
user:
type: string
policy:
type: string
certConfig:
properties:
commonName:
Expand Down Expand Up @@ -4938,6 +4945,8 @@ spec:
type: array
provisionedBuckets:
type: boolean
ldapPolicyAttachToSingleUser:
type: boolean
provisionedUsers:
type: boolean
revision:
Expand Down
12 changes: 10 additions & 2 deletions pkg/apis/minio.min.io/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ type TenantDomains struct {
Console string `json:"console,omitempty"`
}

type LDAPPolicyAttach struct {

Check failure on line 75 in pkg/apis/minio.min.io/v2/types.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

exported: exported type LDAPPolicyAttach should have comment or be unexported (revive)
User string `json:"user,omitempty"`
Group string `json:"group,omitempty"`
Policy string `json:"policy,omitempty"`
}

// Features (`features`) - Object describing which MinIO features to enable/disable in the MinIO Tenant. +
type Features struct {
// *Optional* +
Expand Down Expand Up @@ -360,7 +366,8 @@ type TenantSpec struct {
//
// If provided, statefulset will add these volumes. You should set the rules for the corresponding volumes and volume mounts. We will not test this rule, k8s will show the result.
// +optional
AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"`
AdditionalVolumeMounts []corev1.VolumeMount `json:"additionalVolumeMounts,omitempty"`
LDAPPolicyAttachToSingleUser *LDAPPolicyAttach `json:"ldapPolicyAttachToSingleUser,omitempty"`
}

// Logging describes Logging for MinIO tenants.
Expand Down Expand Up @@ -545,7 +552,8 @@ type TenantStatus struct {
//
// Health Message regarding the State of the tenant
// ProvisionedBuckets keeps track for telling if operator already created initial buckets for the tenant
ProvisionedBuckets bool `json:"provisionedBuckets,omitempty"`
ProvisionedBuckets bool `json:"provisionedBuckets,omitempty"`
LDAPPolicyAttachToSingleUser bool `json:"ldapPolicyAttachToSingleUser,omitempty"`
}

// CertificateConfig (`certConfig`) defines controlling attributes associated to any TLS certificate automatically generated by the Operator as part of tenant creation. These fields have no effect if `spec.autoCert: false`.
Expand Down
10 changes: 8 additions & 2 deletions pkg/controller/main-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/minio/operator/pkg/utils"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/minio/operator/pkg/utils"

"github.com/minio/madmin-go/v2"
"github.com/minio/operator/pkg/common"
xcerts "github.com/minio/pkg/certs"
Expand Down Expand Up @@ -1381,6 +1380,13 @@ func (c *Controller) syncHandler(key string) (Result, error) {
c.RegisterEvent(ctx, tenant, corev1.EventTypeNormal, "BucketsCreated", "Buckets created")
}

// Section to attach LDAP Policy to a user.
if tenant.Status.LDAPPolicyAttachToSingleUser {
klog.Info("A user already got the LDAP policy attached")
} else if len(tenant.Spec.LDAPPolicyAttachToSingleUser.User) > 0 {
c.ldapPolicyAttachToSingleUser(ctx, tenant)
}

// Finally, we update the status block of the Tenant resource to reflect the
// current state of the world
tenant, err = c.updateTenantStatus(ctx, tenant, StatusInitialized, totalAvailableReplicas)
Expand Down
133 changes: 133 additions & 0 deletions pkg/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
corev1 "k8s.io/api/core/v1"
runetimetime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"net"
"net/http"
"strings"
"time"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -314,6 +319,134 @@ func (c *Controller) createUsers(ctx context.Context, tenant *miniov2.Tenant, te
return err
}

func (c *Controller) ldapPolicyAttachToSingleUser(ctx context.Context, tenant *miniov2.Tenant) (err error) {
// This commands has not been executed yet and they were requested
klog.Info("mc_command: There is a mc command to execute")
// For now, we are using the mc binary on the first pod of the first pool.
// If proposal is approved, we can proceed to improve it further.
tenantName := tenant.Name
tenantNamespace := tenant.Namespace
pool0Name := tenant.Spec.Pools[0].Name
podName := tenantName + "-" + pool0Name + "-0"
request := c.kubeClientSet.CoreV1().RESTClient().Post().Resource("pods").Name(podName).Namespace(
tenantNamespace).SubResource("exec")
scheme := runetimetime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
panic("some issue")
}
parameterCodec := runetimetime.NewParameterCodec(scheme)
LDAPPolicyAttachToSingleUser := tenant.Spec.LDAPPolicyAttachToSingleUser
klog.Info(LDAPPolicyAttachToSingleUser)
User := LDAPPolicyAttachToSingleUser.User
klog.Info(User)
Policy := LDAPPolicyAttachToSingleUser.Policy
klog.Info(Policy)
TenantVars := tenant.GetEnvVars()
klog.Info(TenantVars)
valye := tenant.MinIOServerHostAddress()
MinIOServerEndpoint := tenant.MinIOServerEndpoint()
tenant.ConfigurationSecretName()
userCredentials := c.fetchUserCredentials(ctx, tenant)
tenant.HasConfigurationSecret()
tenantSpecConfigName := tenant.Spec.Configuration.Name
klog.Info(tenantSpecConfigName)
usersss := tenant.Spec.Users
klog.Info(usersss)
secret, _ := c.kubeClientSet.CoreV1().Secrets(tenant.Namespace).Get(ctx, tenantSpecConfigName, metav1.GetOptions{})
secretData := secret.Data
content := string(secretData["config.env"])
contents := strings.Split(content, "\n")
klog.Info(contents)
root_access_key := ""

Check failure on line 360 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var root_access_key should be rootAccessKey (revive)
root_secret_key := ""

Check failure on line 361 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var root_secret_key should be rootSecretKey (revive)
for i := range contents {
contenido := contents[i]
klog.Info(contenido)
if strings.Contains(contenido, "MINIO_ROOT_USER") {
valor := strings.Split(contenido, "=")
usuario := valor[1]
usuarioPelon := usuario[1 : len(usuario)-1]
klog.Info(usuarioPelon)
root_access_key = usuarioPelon
}
if strings.Contains(contenido, "MINIO_ROOT_PASSWORD") {
valor := strings.Split(contenido, "=")
password := valor[1]
passwordPelon := password[1 : len(password)-1]
klog.Info(passwordPelon)
root_secret_key = passwordPelon
}
}
klog.Info(root_access_key)
klog.Info(root_secret_key)
klog.Info(MinIOServerEndpoint)
klog.Info(valye)
console_access_key := string(userCredentials[0].Data["CONSOLE_ACCESS_KEY"])

Check failure on line 384 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var console_access_key should be consoleAccessKey (revive)
console_secret_key := string(userCredentials[0].Data["CONSOLE_SECRET_KEY"])

Check failure on line 385 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var console_secret_key should be consoleSecretKey (revive)
klog.Info(console_access_key)
klog.Info(console_secret_key)
// export set HOME=/tmp is to allow mc to work when not using root user in security context to avoid
// mc: <ERROR> Unable to save new mc config. mkdir /.mc: permission denied.
alias := "export set HOME=/tmp; mc alias set myminio " + MinIOServerEndpoint + " " + root_access_key + " " + root_secret_key
ldpapolicyattachtosingleuser_command := "mc idp ldap policy attach myminio " + Policy + " --user=" + "'" + User + "'"

Check failure on line 391 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var ldpapolicyattachtosingleuser_command should be ldpapolicyattachtosingleuserCommand (revive)
klog.Info(ldpapolicyattachtosingleuser_command)
klog.Info(alias)
final_command := alias + "; " + ldpapolicyattachtosingleuser_command

Check failure on line 394 in pkg/controller/operator.go

View workflow job for this annotation

GitHub Actions / lint (1.21.x, ubuntu-latest)

var-naming: don't use underscores in Go names; var final_command should be finalCommand (revive)
cmds := []string{
"sh",
"-c",
final_command,
}
request.VersionedParams(&corev1.PodExecOptions{
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
Container: "minio",
Command: cmds,
}, parameterCodec)
// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
restConfig, err := kubeconfig.ClientConfig()
if err != nil {
panic(err)
}
exec, err := remotecommand.NewSPDYExecutor(restConfig, "POST", request.URL())
fmt.Println(exec)
if err != nil {
panic(err.Error())
}
var stdout, stderr bytes.Buffer
fmt.Println(stdout)
fmt.Println(stderr)
err = exec.Stream(remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdout,
Stderr: &stderr,
Tty: false,
})
if err != nil {
klog.Info(err)
}
actualOutput := stdout.String()
actualError := stderr.String()
klog.Infof("Output from pod: %v", actualOutput)
klog.Infof("Error from pod: %v", actualError)
// If expected output is contained by the actual output, it will be
// considered as a success.
if strings.Contains(actualError, "The specified policy change is already in effect") {
klog.Info("Command(s) succeeded, will not be executed again")
// By saving the command in the status, we guarantee that will not be re-executed.
if _, err = c.updateLDAPPolicyAttachToSingleUserStatus(ctx, tenant, true); err != nil {
klog.V(2).Infof(err.Error())
}
}
return nil
}

func (c *Controller) createBuckets(ctx context.Context, tenant *miniov2.Tenant, tenantConfiguration map[string][]byte) (err error) {
defer func() {
if err == nil {
Expand Down
27 changes: 27 additions & 0 deletions pkg/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ func (c *Controller) updateTenantStatus(ctx context.Context, tenant *miniov2.Ten
return c.updateTenantStatusWithRetry(ctx, tenant, currentState, availableReplicas, true)
}

func (c *Controller) updateLDAPPolicyAttachToSingleUserStatus(ctx context.Context, tenant *miniov2.Tenant, succeeded bool) (*miniov2.Tenant, error) {
return c.updateLDAPPolicyAttachToSingleUserWithRetry(ctx, tenant, succeeded, true)
}

func (c *Controller) updateLDAPPolicyAttachToSingleUserWithRetry(ctx context.Context, tenant *miniov2.Tenant, succeeded bool, retry bool) (
*miniov2.Tenant, error) {
tenantCopy := tenant.DeepCopy()
tenantCopy.Spec = miniov2.TenantSpec{}
tenantCopy.Status = *tenant.Status.DeepCopy()
tenantCopy.Status.LDAPPolicyAttachToSingleUser = succeeded
opts := metav1.UpdateOptions{}
t, err := c.minioClientSet.MinioV2().Tenants(tenant.Namespace).UpdateStatus(ctx, tenantCopy, opts)
t.EnsureDefaults()
if err != nil {
if k8serrors.IsConflict(err) && retry {
klog.Info("Hit conflict issue, getting latest version of tenant")
tenant, err = c.minioClientSet.MinioV2().Tenants(tenant.Namespace).Get(ctx, tenant.Name, metav1.GetOptions{})
if err != nil {
return tenant, err
}
return c.updateLDAPPolicyAttachToSingleUserWithRetry(ctx, tenant, succeeded, false)
}
return t, err
}
return t, nil
}

func (c *Controller) updateTenantStatusWithRetry(ctx context.Context, tenant *miniov2.Tenant, currentState string, availableReplicas int32, retry bool) (*miniov2.Tenant, error) {
// If we are updating the tenant with the same status as before we are going to skip it as to avoid a resource number
// change and have the operator loop re-processing the tenant endlessly
Expand Down

0 comments on commit 8eb0d3d

Please sign in to comment.