Skip to content

Commit

Permalink
Merge pull request #9 from jwcesign/main
Browse files Browse the repository at this point in the history
feat: add instance types provider
  • Loading branch information
jwcesign authored Feb 16, 2025
2 parents 40b6ae7 + 154359b commit 697835a
Show file tree
Hide file tree
Showing 4,844 changed files with 1,768,483 additions and 16 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
31 changes: 31 additions & 0 deletions .github/workflows/presubmit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Pre-submit check

on:
push:
branches: [ "main" ]
pull_request: {}

permissions:
contents: read
pull-requests: read

jobs:
presubmit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true

- uses: actions/setup-go@v5
with:
go-version: '1.23'
cache: false

- name: Install toolchain
run: |
make toolchain
- name: Presubmit check
run: |
make presubmit
15 changes: 14 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/cloudpilot-ai/karpenter-provider-gcp
go 1.23.2

require (
cloud.google.com/go/compute v1.23.3
github.com/awslabs/operatorpkg v0.0.0-20241205163410-0fff9f28d115
github.com/cloudpilot-ai/karpenter-provider-alibabacloud v0.1.4
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/samber/lo v1.47.0
google.golang.org/api v0.149.0
k8s.io/api v0.32.1
k8s.io/apimachinery v0.32.1
k8s.io/client-go v0.32.1
Expand All @@ -15,6 +18,7 @@ require (
)

require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand All @@ -27,20 +31,23 @@ require (
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand All @@ -49,7 +56,9 @@ require (
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
Expand All @@ -59,6 +68,10 @@ require (
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.9.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
105 changes: 105 additions & 0 deletions go.sum

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions pkg/auth/credential.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2025 The CloudPilot AI Authors.
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.
*/

package auth

import "google.golang.org/api/option"

type Credential struct {
ProjectID string
Region string
CredentialOptions option.ClientOption
}
24 changes: 24 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2025 The CloudPilot AI Authors.
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.
*/

package cache

import "time"

const (
// DefaultCleanupInterval triggers cache cleanup (lazy eviction) at this interval.
DefaultCleanupInterval = 1 * time.Minute
)
79 changes: 64 additions & 15 deletions pkg/providers/instancetype/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@ package instancetype

import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"
compute "cloud.google.com/go/compute/apiv1"
"cloud.google.com/go/compute/apiv1/computepb"
"github.com/patrickmn/go-cache"
"google.golang.org/api/iterator"
"sigs.k8s.io/karpenter/pkg/cloudprovider"

"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/apis/v1alpha1"
"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/auth"
pkgcache "github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/cache"
)

const (
InstanceTypesCacheKey = "gce-instancetypes"
InstanceTypesCacheTTL = 23 * time.Hour
)

type Provider interface {
Expand All @@ -32,33 +46,68 @@ type Provider interface {
}

type DefaultProvider struct {
region string
kubeClient client.Client
authOptions *auth.Credential
vmTypesClient *compute.MachineTypesClient

muCache sync.Mutex
cache *cache.Cache
}

func NewProvider(kubeClient client.Client, region string) *DefaultProvider {
return &DefaultProvider{
kubeClient: kubeClient,
region: region,
func NewProvider(region string, authOptions *auth.Credential) (*DefaultProvider, error) {
vmTypesClient, err := compute.NewMachineTypesRESTClient(context.Background(), authOptions.CredentialOptions)
if err != nil {
return nil, err
}
return &DefaultProvider{
authOptions: authOptions,
vmTypesClient: vmTypesClient,
cache: cache.New(InstanceTypesCacheTTL, pkgcache.DefaultCleanupInterval),
}, nil
}

func (p *DefaultProvider) LivenessProbe(*http.Request) error {
// TODO: Implement me
return nil
}

func (p *DefaultProvider) List(ctx context.Context, kubeletConfiguration *v1alpha1.KubeletConfiguration, nodeClass *v1alpha1.GCENodeClass) ([]*cloudprovider.InstanceType, error) {
func (p *DefaultProvider) List(ctx context.Context, kc *v1alpha1.KubeletConfiguration, nodeClass *v1alpha1.GCENodeClass) ([]*cloudprovider.InstanceType, error) {
// TODO: Implement me
p.muCache.Lock()
defer p.muCache.Unlock()

return nil, nil
}

func (p *DefaultProvider) UpdateInstanceTypes(ctx context.Context) error {
// TODO: Implement me
return nil
}
func (p *DefaultProvider) getInstanceTypes(ctx context.Context) ([]*computepb.MachineType, error) {
p.muCache.Lock()
defer p.muCache.Unlock()

func (p *DefaultProvider) UpdateInstanceTypeOfferings(ctx context.Context) error {
// TODO: Implement me
return nil
if cached, ok := p.cache.Get(InstanceTypesCacheKey); ok {
return cached.([]*computepb.MachineType), nil
}

vmFilter := fmt.Sprintf("(zone eq .*%s-.*)", p.authOptions.Region)
req := &computepb.AggregatedListMachineTypesRequest{
Project: p.authOptions.ProjectID,
Filter: &vmFilter,
}

it := p.vmTypesClient.AggregatedList(ctx, req)

var vmTypes []*computepb.MachineType
for {
resp, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}

if err != nil {
return nil, err
}

vmTypes = append(vmTypes, resp.Value.MachineTypes...)
}

p.cache.SetDefault(InstanceTypesCacheKey, vmTypes)
return vmTypes, nil
}
Loading

0 comments on commit 697835a

Please sign in to comment.