Skip to content

Commit

Permalink
Initial deployer registration commit with older endpoints and components
Browse files Browse the repository at this point in the history
misc: add missing license (cisco-open#173)

Several files that are recently added don't have license statements.
license statements are added in those files.

Making docker driver default. (cisco-open#171)

Updated endpoints and components, removed registration-db controller code
  • Loading branch information
dhruvsgarg committed Jul 12, 2022
1 parent 53594f9 commit c63a1c8
Show file tree
Hide file tree
Showing 26 changed files with 539 additions and 98 deletions.
12 changes: 2 additions & 10 deletions api/computes_api.partials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# SPDX-License-Identifier: Apache-2.0

/computes/{compute}:
/computes:
########################################
# Register a new compute cluster
########################################
Expand All @@ -23,15 +23,6 @@
summary: Register a new compute cluster
tags:
- computes
parameters:
- name: compute
description: deployer id of compute cluster
in: path
schema:
type: string
style: simple
explode: false
required: true
requestBody:
content:
application/json:
Expand All @@ -49,6 +40,7 @@
$ref: '#/components/schemas/Error'
description: unexpected error

/computes/{compute}:
########################################
# Get the status of a compute cluster
########################################
Expand Down
25 changes: 6 additions & 19 deletions api/computes_components.partials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ ComputeSpec:
description: Compute specification
type: object
properties:
deployerId:
type: string
adminId:
type: string
region:
Expand All @@ -31,14 +29,11 @@ ComputeSpec:
type: string
apiKey:
type: string
required:
- deployerId
example:
deployerId: "60d0d66716af13b787d9ef0a"
adminId: "admin1"
region: "us-east"
computeId: "1"
apiKey: "892nsfd2ds82"
computeId: "62cdd50e5e28e1473c3768fc"

########################################
# Compute status
Expand All @@ -47,27 +42,19 @@ ComputeStatus:
description: Cluster compute status
type: object
properties:
deployerId:
type: string
state:
$ref: '#/components/schemas/ComputeState'
registeredAt:
type: string
format: date-time
state:
$ref: '#/components/schemas/ComputeState'
updatedAt:
type: string
format: date-time
region:
type: string
computeId:
type: string
computeSpec:
type: string
spec:
$ref: '#/components/schemas/ComputeSpec'
required:
- deployerId
- state
- region
- computeId
- spec

########################################
# Compute state
Expand Down
1 change: 1 addition & 0 deletions cmd/controller/app/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (c *Controller) serveRestApi() {
openapi.NewDesignCodesApiController(controller.NewDesignCodesApiService(c.dbService)),
openapi.NewDesignSchemasApiController(controller.NewDesignSchemasApiService(c.dbService)),
openapi.NewJobsApiController(controller.NewJobsApiService(c.dbService, c.jobEventQ, c.jobBuilder)),
openapi.NewComputesApiController(controller.NewComputesApiService(c.dbService)),
}

router := openapi.NewRouter(apiRouters...)
Expand Down
6 changes: 6 additions & 0 deletions cmd/controller/app/database/db_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DBService interface {
DesignService
JobService
TaskService
ComputeService
}

// DatasetService is an interface that defines a collection of APIs related to dataset
Expand Down Expand Up @@ -80,3 +81,8 @@ type TaskService interface {
UpdateTaskStateByFilter(string, openapi.JobState, map[string]interface{}) error
UpdateTaskStatus(string, string, openapi.TaskStatus) error
}

// ComputeService is an interface that defines a collection of APIs related to computes
type ComputeService interface {
RegisterCompute(openapi.ComputeSpec) (openapi.ComputeStatus, error)
}
91 changes: 91 additions & 0 deletions cmd/controller/app/database/mongodb/compute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package mongodb

import (
"context"
"fmt"
"time"

"github.com/cisco-open/flame/pkg/openapi"
"github.com/cisco-open/flame/pkg/util"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
)

// RegisterCompute creates a new cluster compute specification and returns ComputeStatus
func (db *MongoService) RegisterCompute(computeSpec openapi.ComputeSpec) (openapi.ComputeStatus, error) {
// override userId in jobSpec to prevent an incorrect record in the db
result, err := db.jobCollection.InsertOne(context.TODO(), computeSpec)
if err != nil {
zap.S().Warnf("Failed to create a new job: %v", err)

return openapi.ComputeStatus{}, ErrorCheck(err)
}

updatedSpec := computeSpec
updatedSpec.ComputeId = GetStringID(result.InsertedID)

computeStatus := openapi.ComputeStatus{
State: openapi.UP,
Spec: updatedSpec,
RegisteredAt: time.Now(),
}

err = db.UpdateComputeStatus(computeStatus.Spec.ComputeId, computeStatus)
if err != nil {
zap.S().Warnf("Failed to update compute status: %v", err)

return openapi.ComputeStatus{}, ErrorCheck(err)
}

zap.S().Infof("Successfully registered a new compute cluster with computeID %s", computeStatus.Spec.ComputeId)

return computeStatus, err
}

// UpdateJobStatus update Job's status
func (db *MongoService) UpdateComputeStatus(computeId string, computeStatus openapi.ComputeStatus) error {
dateKey := ""
switch computeStatus.State {
case openapi.UP:
fallthrough
case openapi.DOWN:
fallthrough
case openapi.MAINTENANCE:
dateKey = updatedAt

default:
return fmt.Errorf("unknown state: %s", computeStatus.State)
}

setElements := bson.M{util.DBFieldId: computeId, "state": computeStatus.State}
if dateKey != "" {
setElements[dateKey] = time.Now()
}

filter := bson.M{util.DBFieldMongoID: ConvertToObjectID(computeId)}
update := bson.M{"$set": setElements}

updatedDoc := openapi.ComputeStatus{}
err := db.jobCollection.FindOneAndUpdate(context.TODO(), filter, update).Decode(&updatedDoc)
if err != nil {
return ErrorCheck(err)
}

return nil
}
1 change: 1 addition & 0 deletions cmd/controller/app/database/mongodb/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
latestVersion = "latest"
unknownVersion = "unknown"
updatedAt = "updatedat"
)

/*
Expand Down
2 changes: 1 addition & 1 deletion cmd/controller/app/database/mongodb/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (db *MongoService) UpdateJobStatus(userId string, jobId string, jobStatus o
dateKey = "startedat"

case openapi.APPLYING:
dateKey = "updatedat"
dateKey = updatedAt

case openapi.FAILED:
fallthrough
Expand Down
16 changes: 16 additions & 0 deletions cmd/deployer/app/appserver.go
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package app
80 changes: 80 additions & 0 deletions cmd/deployer/app/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package app

import (
"crypto/tls"
"net/http"
"os"

"github.com/cisco-open/flame/pkg/openapi"
"github.com/cisco-open/flame/pkg/restapi"
"go.uber.org/zap"
)

type ComputeResource struct {
apiserverEp string
name string
spec openapi.ComputeSpec
registered bool
}

func NewCompute(apiserverEp string, computeSpec openapi.ComputeSpec, bInsecure bool, bPlain bool) (*ComputeResource, error) {
name, err := os.Hostname()
if err != nil {
return nil, err
}

if bPlain {
} else {
tlsCfg := &tls.Config{}
if bInsecure {
zap.S().Warn("Warning: allow insecure connection\n")

tlsCfg.InsecureSkipVerify = true
http.DefaultTransport.(*http.Transport).TLSClientConfig = tlsCfg
}
}

compute := &ComputeResource{
apiserverEp: apiserverEp,
name: name,
spec: computeSpec,
registered: false,
}

return compute, nil
}

func (compute *ComputeResource) RegisterNewCompute() error {
// construct URL
uriMap := map[string]string{}
url := restapi.CreateURL(compute.apiserverEp, restapi.RegisterComputeEndpoint, uriMap)

code, resp, err := restapi.HTTPPost(url, compute.spec, "application/json")
if err != nil || restapi.CheckStatusCode(code) != nil {
zap.S().Errorf("Failed to register compute, sent computeSpec: %v, resp: %v, code: %d, err: %v",
compute.spec, string(resp), code, err)
return err
}

zap.S().Infof("Success in registering new compute, sent obj: %v, resp: %v, code: %d",
compute.spec, string(resp), code)
compute.registered = true

return nil
}
16 changes: 16 additions & 0 deletions cmd/deployer/app/service.go
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

package app
Loading

0 comments on commit c63a1c8

Please sign in to comment.