/
http_handlers.go
101 lines (89 loc) · 3.37 KB
/
http_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// This file is part of MinIO Operator
// Copyright (c) 2023 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sidecar
//lint:file-ignore ST1005 Incorrectly formatted error string
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/TheCodeCargo/minio-operator/pkg/resources/services"
"github.com/gorilla/mux"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)
// BucketSrvHandler - POST /webhook/v1/bucketsrv/{namespace}/{name}?bucket={bucket}
func (c *Controller) BucketSrvHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
v := r.URL.Query()
namespace := vars["namespace"]
bucket := vars["bucket"]
name := vars["name"]
deleteBucket := v.Get("delete")
ok, err := strconv.ParseBool(deleteBucket)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
if ok {
if err = c.kubeClient.CoreV1().Services(namespace).Delete(r.Context(), bucket, metav1.DeleteOptions{}); err != nil {
klog.Errorf("failed to delete service:%s for tenant:%s/%s, err:%s", name, namespace, name, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
return
}
// Find the tenant
tenant, err := c.controllerClient.MinioV2().Tenants(namespace).Get(r.Context(), name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Unable to lookup tenant:%s/%s for the bucket:%s request. err:%s", namespace, name, bucket, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tenant.EnsureDefaults()
// Validate the MinIO Tenant
if err = tenant.Validate(); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
ok, error := validateBucketName(bucket)
if !ok {
http.Error(w, error.Error(), http.StatusBadRequest)
return
}
// Create the service for the bucket name
service := services.ServiceForBucket(tenant, bucket)
_, err = c.kubeClient.CoreV1().Services(namespace).Create(r.Context(), service, metav1.CreateOptions{})
if err != nil && k8serrors.IsAlreadyExists(err) {
klog.Infof("Bucket:%s already exists for tenant:%s/%s err:%s ", bucket, namespace, name, err)
// This might be a previously failed bucket creation. The service is expected to be the same as the one
// already in place so clear the error.
err = nil
}
if err != nil {
klog.Errorf("Unable to create service for tenant:%s/%s for the bucket:%s request. err:%s", namespace, name, bucket, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func validateBucketName(bucket string) (bool, error) {
// Additional check on top of existing checks done by minio due to limitation of service creation in k8s
if strings.Contains(bucket, ".") {
return false, fmt.Errorf("invalid bucket name: . in bucket name: %s", bucket)
}
return true, nil
}