Skip to content

Commit

Permalink
Revert "wip"
Browse files Browse the repository at this point in the history
This reverts commit e9b81fe.
  • Loading branch information
Mattes83 committed Jul 4, 2024
1 parent 8c61525 commit 1742abd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 176 deletions.
70 changes: 11 additions & 59 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ type userpassword struct {
Tokens []string `json:"tokens,omitempty"`
}

type Server struct {
Name string
ProviderID string
DatacenterID string
}

func New(datacenterId string, secret []byte) (IONOSClient, error) {
var cfg *ionoscloud.Configuration
if secret[0] == '{' {
Expand Down Expand Up @@ -71,77 +65,43 @@ func (a *IONOSClient) GetServer(ctx context.Context, providerID string) (*cloudp
return a.convertServerToInstanceMetadata(ctx, &server)
}

func (a *IONOSClient) RemoveIPFromNode(ctx context.Context, loadBalancerIP, providerID string) error {
if a.client == nil {
return errors.New("client isn't initialized")
}

serverReq := a.client.NetworkInterfacesApi.DatacentersServersNicsGet(ctx, a.DatacenterId, providerID)
nics, req, err := serverReq.Depth(3).Execute()
if err != nil {
if req != nil && req.StatusCode == 404 {
return nil
}
return err
}

if !nics.HasItems() {
return errors.New("node has no nics")
}

primaryNic := (*nics.Items)[0]
ips := *primaryNic.Properties.Ips

for idx, v := range ips {
if v == loadBalancerIP {
ips = append(ips[:idx], ips[idx+1:]...)
}
}

_, _, err = a.client.NetworkInterfacesApi.DatacentersServersNicsPatch(ctx, a.DatacenterId, providerID, *primaryNic.Id).Nic(ionoscloud.NicProperties{
Ips: &ips,
}).Execute()

return err
}

func (a *IONOSClient) AttachIPToNode(ctx context.Context, loadBalancerIP, providerID string) (bool, error) {
if a.client == nil {
return false, errors.New("client isn't initialized")
}

serverReq := a.client.NetworkInterfacesApi.DatacentersServersNicsGet(ctx, a.DatacenterId, providerID)
nics, req, err := serverReq.Depth(3).Execute()
if err != nil {
if req != nil && req.StatusCode == 404 {
return false, nil
}
if req != nil && req.StatusCode == 404 {
return false, err
}

if !nics.HasItems() {
return false, errors.New("node has no nics")
return false, nil
}

primaryNic := (*nics.Items)[1]
primaryNic := (*nics.Items)[0]
ips := *primaryNic.Properties.Ips
ips = append(ips, loadBalancerIP)

_, _, err = a.client.NetworkInterfacesApi.DatacentersServersNicsPatch(ctx, a.DatacenterId, providerID, *primaryNic.Id).Nic(ionoscloud.NicProperties{
Ips: &ips,
}).Execute()

return true, err
return err != nil, err
}

func (a *IONOSClient) GetServerByIP(ctx context.Context, loadBalancerIP string) (*Server, error) {
func (a *IONOSClient) GetServerByIP(ctx context.Context, loadBalancerIP string) (*string, error) {
if a.client == nil {
return nil, errors.New("client isn't initialized")
}

serverReq := a.client.ServersApi.DatacentersServersGet(ctx, a.DatacenterId)
servers, _, err := serverReq.Depth(3).Execute()
if err != nil {
servers, req, err := serverReq.Depth(3).Execute()
if err != nil || req != nil && req.StatusCode == 404 {
if err != nil {
return nil, nil
}
return nil, err
}

Expand All @@ -150,27 +110,19 @@ func (a *IONOSClient) GetServerByIP(ctx context.Context, loadBalancerIP string)
}

for _, server := range *servers.Items {
klog.Infof("Checking server %s", server.Properties.Name)
if !server.Entities.HasNics() {
continue
}
for _, nic := range *server.Entities.Nics.Items {
if nic.Properties.HasIps() {
for _, ip := range *nic.Properties.Ips {
klog.Infof("Found ip %s", ip)
if loadBalancerIP == ip {
klog.Info("Its a match!")
return &Server{
Name: *server.Properties.Name,
ProviderID: *server.Id,
DatacenterID: a.DatacenterId,
}, nil
return server.Properties.Name, nil
}
}
}
}
}
klog.Infof("IP %s not found on any node in datacenter %s", loadBalancerIP, a.DatacenterId)

return nil, nil
}
Expand Down
24 changes: 10 additions & 14 deletions pkg/ionos/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ionos

import (
"context"
"encoding/json"
client2 "github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/client"
"github.com/GDATASoftwareAG/cloud-provider-ionoscloud/pkg/config"
"io"
Expand All @@ -12,21 +13,16 @@ import (

func init() {
cloudprovider.RegisterCloudProvider(config.RegisteredProviderName, func(cfg io.Reader) (cloudprovider.Interface, error) {
//
//byConfig, err := io.ReadAll(cfg)
//if err != nil {
// klog.Errorf("ReadAll failed: %s", err)
// return nil, err
//}
//var conf config.Config
//err = json.Unmarshal(byConfig, &conf)
//if err != nil {
// return nil, err
//}

byConfig, err := io.ReadAll(cfg)
if err != nil {
klog.Errorf("ReadAll failed: %s", err)
return nil, err
}
var conf config.Config
conf.TokenSecretNamespace = "kube-system"
conf.TokenSecretName = "ionos-cpi-secret"
err = json.Unmarshal(byConfig, &conf)
if err != nil {
return nil, err
}

return newProvider(conf), nil
})
Expand Down
Loading

0 comments on commit 1742abd

Please sign in to comment.