Skip to content

Commit

Permalink
Add API-Server
Browse files Browse the repository at this point in the history
  • Loading branch information
akuangkkk committed Dec 20, 2023
1 parent 0b8405b commit 5abde7b
Show file tree
Hide file tree
Showing 113 changed files with 73,012 additions and 10 deletions.
Binary file modified .DS_Store
Binary file not shown.
17 changes: 11 additions & 6 deletions Documentation/devhandbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ Build dokcer image by running `docker build -t <docker username>/alluxio-operato

* Example:
```shell
docker buildx build --platform linux/amd64 -t kshou433/alluxio-operator:v1.6 -f dev/build/Dockerfile .
docker buildx build --platform linux/amd64 -t kshou433/alluxio-operator:withAPIServer_v2.0 -f dev/build/Dockerfile .
```

### Step 3
Push image to docker hub : `docker push <docker username>/alluxio-operator:<tag>`.

* Example:
```shell
docker push kshou433/alluxio-operator:v1.6
docker push kshou433/alluxio-operator:withAPIServer_v2.0
```

### Step 4
Expand All @@ -53,13 +53,18 @@ Update image url and tage in ```operator-config.yaml```
## Verify the Deployment

### Check endpoints
```kubectl get endpoints -A```
`kubectl get endpoints -A`

### Check if prometheus is running
```kubectl get prometheus -o yaml```
`kubectl get prometheus -o yaml`

### Check if several prometheus operators are running on the cluster:
```kubectl get pods --all-namespaces | grep 'prom.*operator'```
`kubectl get pods --all-namespaces | grep 'prom.*operator'`

### Access prometheus GUI
```kubectl -n monitoring port-forward svc/prometheus-k8s 9090```
`kubectl -n monitoring port-forward svc/prometheus-k8s 9090`

### Access API Server Port
`kubectl port-forward -n alluxio-operator <api-server-controller Pod Name> 5220:5220`

Then use `curl` to do REST API Request.
5 changes: 2 additions & 3 deletions Documentation/notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Notes
Change generate file, https://book.kubebuilder.io/quick-start.html?highlight=docker#run-it-on-the-cluster

add prometheus folder and monitor yaml. https://book.kubebuilder.io/reference/metrics

check rbac folder https://book.kubebuilder.io/reference/metrics-reference
API Server needs a `deployment` config yaml file.

Binary file added cmd/api_server/api_server/.DS_Store
Binary file not shown.
97 changes: 97 additions & 0 deletions cmd/api_server/api_server/endpoints/alluxio_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package endpoints

import (
"fmt"
"github.com/Alluxio/k8s-operator/api/v1alpha1"
"github.com/emicklei/go-restful"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type AlluxioClusterEndpoint struct {
client client.Client
}

func NewAlluxioClusterEndpoint(client client.Client) *AlluxioClusterEndpoint {
return &AlluxioClusterEndpoint{client: client}
}

func (alluxioClusterEndpoint *AlluxioClusterEndpoint) SetupWithWS(ws *restful.WebService) {
ws.Route(ws.GET("alluxio_cluster").To(alluxioClusterEndpoint.show).
Doc("List of AlluxioClusters").
Returns(200, "OK", &v1alpha1.AlluxioClusterList{}))

ws.Route(ws.POST("alluxio_cluster").To(alluxioClusterEndpoint.create).
Doc("Create AlluxioClusters").
Returns(200, "OK", nil).
Returns(400, "Bad Request", nil))

ws.Route(ws.DELETE("alluxio_cluster").To(alluxioClusterEndpoint.delete).
Doc("Delete Current AlluxioClusters").
Returns(200, "OK", nil).
Returns(400, "Bad Request", nil))

ws.Route(ws.PATCH("alluxio_cluster").To(alluxioClusterEndpoint.update).
Doc("Update Current AlluxioClusters").
Returns(200, "OK", nil).
Returns(400, "Bad Request", nil))

}

func (alluxioClusterEndpoint *AlluxioClusterEndpoint) show(request *restful.Request, response *restful.Response) {
// Get AlluxioClusterList
AlluxioClusterList := new(v1alpha1.AlluxioClusterList)
err := alluxioClusterEndpoint.client.List(request.Request.Context(), AlluxioClusterList, &client.ListOptions{})
// Unable to get:
if err != nil {
writeError(response, 404, Error{
Title: "Error",
Details: fmt.Sprintf("Could not retrieve list: %s", err),
})
return
}
// Write to response:
newAL := AlluxioClusterConverter.AlluxioClusterList(AlluxioClusterList)
if err := response.WriteAsJson(newAL); err != nil {
writeError(response, 404, Error{
Title: "Error",
Details: "Could not list resources",
})
}
}

type Test struct {
Title string `json:"title"`
}

func (alluxioClusterEndpoint *AlluxioClusterEndpoint) create(request *restful.Request, response *restful.Response) {
test := new(Test)

err := request.ReadEntity(test)

if err != nil {
writeError(response, 400, Error{
Title: "Bad Request",
Details: "Could not read entity",
})
return
}

writeError(response, 400, Error{
Title: "Error",
Details: "To Do, can read entity",
})
}

func (alluxioClusterEndpoint *AlluxioClusterEndpoint) update(request *restful.Request, response *restful.Response) {
writeError(response, 400, Error{
Title: "Error",
Details: "To Do",
})
}

func (alluxioClusterEndpoint *AlluxioClusterEndpoint) delete(request *restful.Request, response *restful.Response) {
writeError(response, 400, Error{
Title: "Error",
Details: "To Do",
})
}
140 changes: 140 additions & 0 deletions cmd/api_server/api_server/endpoints/dataset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package endpoints

import (
"fmt"
"github.com/Alluxio/k8s-operator/api/v1alpha1"
"github.com/emicklei/go-restful"
"sigs.k8s.io/controller-runtime/pkg/client"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type DatasetEndpoint struct {
client client.Client
}

func NewDataSetEndpoint(client client.Client) *DatasetEndpoint {
return &DatasetEndpoint{client: client}
}

func (datasetEndpoint *DatasetEndpoint) SetupWithWS(ws *restful.WebService) {
ws.Route(ws.GET("dataset").To(datasetEndpoint.show).
Doc("List of Datasets").
Returns(200, "OK", DatasetList{}))

ws.Route(ws.POST("dataset").To(datasetEndpoint.create).
Doc("Create a new Dataset").
Returns(200, "OK", Dataset{}).
Returns(400, "Bad Request", nil))

ws.Route(ws.DELETE("dataset").To(datasetEndpoint.delete).
Doc("Delete Current Dataset").
Returns(200, "OK", nil).
Returns(400, "Bad Request", nil))

// todo add more route
}

func (datasetEndpoint *DatasetEndpoint) show(request *restful.Request, response *restful.Response) {
datasetList := new(v1alpha1.DatasetList)
err := datasetEndpoint.client.List(request.Request.Context(), datasetList, &client.ListOptions{})

if err != nil {
writeError(response, 404, Error{
Title: "Error",
Details: fmt.Sprintf("Could not retrieve list: %s", err),
})
} else {
// Convert
newDL := DatasetConverter.DatasetList(datasetList)
// Send
if err := response.WriteAsJson(newDL); err != nil {
writeError(response, 404, Error{
Title: "Error",
Details: "Could not list resources",
})
}
}

}

func (datasetEndpoint *DatasetEndpoint) create(request *restful.Request, response *restful.Response) {
// Read input
dataset := new(Dataset)
err := request.ReadEntity(dataset)

if err != nil {
writeError(response, 400, Error{
Title: "Bad Request",
Details: "Could not read entity",
})
return
}

// TODO May need validation func

datasetObj := &v1alpha1.Dataset{
ObjectMeta: metav1.ObjectMeta{Name: dataset.Name},
Spec: v1alpha1.DatasetSpec{
Dataset: v1alpha1.DatasetConf{
Path: dataset.Path,
Credentials: dataset.Credentials,
},
},
}

// Deploy the object
err = datasetEndpoint.client.Create(request.Request.Context(), datasetObj, &client.CreateOptions{})
if err != nil {
writeError(response, 400, Error{
Title: "Error",
Details: fmt.Sprintf("Could not create object: %s", err),
})
} else {
// Convert
newDatasetObj := DatasetConverter.DatasetObject(datasetObj)
// Send
if err := response.WriteAsJson(newDatasetObj); err != nil {
writeError(response, 404, Error{
Title: "Error",
Details: "Could not list resources",
})
}
}
}

func (datasetEndpoint *DatasetEndpoint) delete(request *restful.Request, response *restful.Response) {
//writeError(response, 404, Error{
// Title: "Error",
// Details: "To Do",
//})
dataset := new(Dataset)
err := request.ReadEntity(dataset)
if err != nil {
writeError(response, 400, Error{
Title: "Bad Request",
Details: "Could not read entity",
})
return
}

// TODO May need validation func

datasetObj := &v1alpha1.Dataset{
ObjectMeta: metav1.ObjectMeta{Name: dataset.Name},
Spec: v1alpha1.DatasetSpec{
Dataset: v1alpha1.DatasetConf{
Path: dataset.Path,
Credentials: dataset.Credentials,
},
},
}

// Delete the object
if err = datasetEndpoint.client.Delete(request.Request.Context(), datasetObj, &client.DeleteOptions{}); err != nil {
writeError(response, 400, Error{
Title: "Error",
Details: fmt.Sprintf("Could not Delete object: %s", err),
})
}
}
46 changes: 46 additions & 0 deletions cmd/api_server/api_server/endpoints/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package endpoints

import (
"github.com/Alluxio/k8s-operator/api/v1alpha1"
"github.com/emicklei/go-restful"
)

type Endpoint interface {
SetupWithWS(ws *restful.WebService)
}

type Source struct {
Type string `json:"type"`
BaseURL string `json:"baseUrl,omitempty"`
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
Region string `json:"region,omitempty"`
PathPrefix string `json:"pathPrefix,omitempty"`
}

type Error struct {
Title string `json:"title"`
Details string `json:"details"`
}

// AlluxioCluster Part
type AlluxioCluster struct {
Spec *v1alpha1.AlluxioClusterSpec `json:"spec,omitempty"`
Status *v1alpha1.AlluxioClusterStatus `json:"status,omitempty"`
}

type AlluxioClusterList struct {
Items []AlluxioCluster `json:"items"`
}

// Dataset Part
type Dataset struct {
Name string `json:"name"`
Path string `json:"path"`
Credentials map[string]string `json:"credentials,omitempty"`
Status *v1alpha1.DatasetStatus `json:"status,omitempty"`
}

type DatasetList struct {
Items []Dataset `json:"items"`
}
60 changes: 60 additions & 0 deletions cmd/api_server/api_server/endpoints/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package endpoints

import (
"github.com/Alluxio/k8s-operator/api/v1alpha1"
"github.com/emicklei/go-restful"
kubelog "sigs.k8s.io/controller-runtime/pkg/log"
)

// writeError is a helper func that write error message to rest response
func writeError(response *restful.Response, httpStatus int, err Error) {
if err := response.WriteHeaderAndJson(httpStatus, err, "application/json"); err != nil {
kubelog.Log.Error(err, "Could not write the error response")
}
}

// DatasetConverter is used to simplify Dataset CRD to struct in types.go
var AlluxioClusterConverter = &alluxioClusterConverter{}

type alluxioClusterConverter struct{}

func (c *alluxioClusterConverter) AlluxioClusterObject(d *v1alpha1.AlluxioCluster) *AlluxioCluster {
return &AlluxioCluster{
Spec: &d.Spec,
Status: &d.Status,
}
}

func (c *alluxioClusterConverter) AlluxioClusterList(list *v1alpha1.AlluxioClusterList) *AlluxioClusterList {
items := make([]AlluxioCluster, len(list.Items))
for i, r := range list.Items {
items[i] = *c.AlluxioClusterObject(&r)
}
return &AlluxioClusterList{
Items: items,
}
}

// DatasetConverter is used to simplify Dataset CRD to struct in types.go
var DatasetConverter = &datasetConverter{}

type datasetConverter struct{}

func (c *datasetConverter) DatasetObject(d *v1alpha1.Dataset) *Dataset {
return &Dataset{
Name: d.Name,
Path: d.Spec.Dataset.Path,
Credentials: d.Spec.Dataset.Credentials,
Status: &d.Status,
}
}

func (c *datasetConverter) DatasetList(list *v1alpha1.DatasetList) *DatasetList {
items := make([]Dataset, len(list.Items))
for i, r := range list.Items {
items[i] = *c.DatasetObject(&r)
}
return &DatasetList{
Items: items,
}
}
Loading

0 comments on commit 5abde7b

Please sign in to comment.