Using Cluster Optimzier's API
1. Introduction to Cluster Optimizer API
The Cluster Optimizer API is a powerful, RESTful interface that enables users to automate and manage cloud-native cluster optimization tasks. It exposes key functionalities such as cluster management, resource optimization, and cost-saving analysis, making it possible to programmatically interact with the platform. Users can integrate the API into their workflows to enhance automation and reduce the need for manual intervention. Supporting multiple cloud platforms like AWS and Kubernetes, the API is designed to scale with modern cloud environments.
2. Accessing Cluster Optimizer API
To get started with the Cluster Optimizer API, it’s essential to understand how to generate client code, authenticate requests, and invoke key endpoints. This section covers the basics needed to interact programmatically with the API.
2.1. Generate Client Code Using Swagger Codegen
Cluster Optimizer API is well-documented using OpenAPI (Swagger) specifications, which makes generating client code straightforward. With Swagger Codegen, developers can automatically create client libraries in various programming languages, such as Go, Python, or Java. The following steps demonstrate how to generate a client for the API using go language:
-
Download the Management API or the Optimize API OpenAPI spec from the Cluster Optimizer’s API documentation.
-
Install Swagger Codegen.
git clone https://github.com/swagger-api/swagger-codegen.git cd swagger-codegen mvn clean install
-
Run the following command to generate client code in the go language:
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \ -i management-api.json -l go \ -o ~/swagger-codegen/optimize-api-go-client
-
Import the generated client into your project to streamline API interactions.
2.2. Authentication
To securely access Cluster Optimizer’s API, a token-based authentication mechanism is used. You need to create an access key and access key secret, and then construct access token to authenticate requests.
1. Create an Access Key
-
Login into the Cluster Optimizer web console, Click on your user avatar in the top left navigation bar.
-
From the expanded menu, select
Personal Settings
to access the personal settings page. -
On the personal settings page, click the
Access Keys
tab to view and manage your access keys, as illustrated below.Access Key List
-
Click the
New
button at the top right of theAccess Keys
table. It will automatic generate the access key and list it in the table. -
Remeber the access key and access key secret, as they will be used to generate the access token.
2. Generate a Message to Sign
Before generating a signed API key, you need a message that combines Access Key, Timestamp and Nonce with :
as a separator:
- Access Key: The public identifier of the client.
- Timestamp: To prevent replay attacks, include a timestamp (in nanoseconds).
- Nonce: A random string to prevent replay attacks.
For example (in Go language):
message:=fmt.Sprintf("%s:%d:%s", accessKeyID, timestamp, nonce)
In this example, accessKeyID
is the access key ID of the client, timestamp
is the timestamp in nanoseconds, and nonce
is a random string.
3. Sign the Message with HMAC-SHA256
The next step is to use the API key secret to generate a signature using HMAC-SHA256. This signature ensures the request has not been tampered with and that the client is legitimate.
Example (in Go language) is as follows:
func generateSignature( keyID string, keySecret string, timestamp int64, nonce string) (string, error) {
message:=fmt.Sprintf("%s:%d:%s", keyID, timestamp, nonce)
h:=hmac.New(sha256.New, []byte(keySecret))
if _, err:=h.Write([]byte(message)); err!=nil {
return"", err
}
signature:=base64.StdEncoding.EncodeToString(h.Sum(nil))
returnsignature, nil
}
This function generates a signature by hashing the message using the API key secret and the HMAC-SHA256 algorithm.
4. Construct the Access Token
After generating the signature, you can construct the final API key by combining the Access Token (public) and the generated signature (private).
Example API request with a signed API key in the header is as follows:
GET /api/resource HTTP/1.1
Host: api.example.com
Authorization: Bearer <accessKey>/<timestamp>/<nonce>/<signature>
In this example:
accessKey
is the public access key.timestamp
: To prevent replay attacks, include a timestamp (in nanoseconds).nonce
: A random string to prevent replay attacks.signature
is the HMAC-SHA256 signed message using the secret.
Example (in Go language) is as follows::
func GenerateAccessToken(keyID string, keySecret string, timestamp int64, nonce string) (string, error) {
if timestamp==0 {
timestamp=time.Now().UTC().UnixNano()
}
signature, err:=generateSignature(keyID, keySecret, timestamp, nonce)
if err!=nil {
return"", err
}
return utils.URLEncode(fmt.Sprintf("%s/t%d/t%s/t%s", keyID, timestamp, nonce, signature)), nil
}
2.3. Invocation
Once authentication is set up, API invocation is simple. Using the generated client, you can perform operations like retrieving cluster details, get resource recommendations, and so on. Here’s an example in Go for listing notification rules:
You can construct an HTTP request using the access token generated and methods provided by generated code using open api specification.
Example (in Go language) is as follows:
client:= NewAPIClient(&Configuration{
BasePath: "http://api.example.com",
})
apiKey:="AKFgGMF3FWWe5mgkz3UWBMgaUgrzBMUV"
apiSecret:="******"
nonce:="NONe5mgkz3GBk"
accessToken, _:= GenerateAccessToken(apiKey, apiSecret, 0, nonce)
ctx:=context.Background()
ctx=context.WithValue(ctx, ContextAccessToken, accessToken)
rules, _, err:=client.NotificationRuleApi.ListNotificationRules(ctx, &NotificationRuleApiListNotificationRulesOpts{})
This code demonstrates how to integrate API calls into your application workflow, making it easy to automate optimizations and management tasks.
3. What is Next?
The REST API is the fundamental fabric of Cluster Optimizer. Cluster Optimizer provides a REST API that allows you to perform the following operations:
- Management API: Manage clusters, cluster access accounts, access keys, notification channels, notification rules and so on.
- Optimize API: Provider recommendation and optimization for kubernetes resources, such as node group, persistent volume, workload and so on.
By integrating these capabilities, organizations can achieve full automation of their cloud-native resources while ensuring cost efficiency.
About the Cluster Optimizer
The Cluster Optimizer, developed by WiseInf, is a comprehensive cloud-native optimization solution designed to help organizations reduce costs and enhance operational efficiency. By analyzing cloud resources, application performance, user behavior, and cloud provider’s data, it identifies cost-saving opportunities and delivers tailored recommendations. The platform also automates the optimization process, minimizing manual errors and streamlining operations to ensure greater efficiency.