44 "context"
55 "fmt"
66 "net/http"
7- "strconv"
87
9- "github.com/linode/linode-cloud-controller-manager/sentry"
108 "github.com/linode/linodego"
119 v1 "k8s.io/api/core/v1"
1210 "k8s.io/apimachinery/pkg/types"
@@ -55,49 +53,36 @@ func (i *instances) nodeAddresses(ctx context.Context, linode *linodego.Instance
5553 return addresses , nil
5654}
5755
58- func (i * instances ) InstanceExists (ctx context.Context , node * v1.Node ) (bool , error ) {
56+ func (i * instances ) lookupLinode (ctx context.Context , node * v1.Node ) (* linodego. Instance , error ) {
5957 providerID := node .Spec .ProviderID
58+ nodeName := types .NodeName (node .Name )
6059
61- ctx = sentry .SetHubOnContext (ctx )
62- sentry .SetTag (ctx , "provider_id" , providerID )
60+ if providerID != "" {
61+ id , err := parseProviderID (providerID )
62+ if err != nil {
63+ return nil , err
64+ }
6365
64- id , err := parseProviderID (providerID )
65- if err != nil {
66- sentry .CaptureError (ctx , err )
67- return false , err
66+ return linodeByID (ctx , i .client , id )
6867 }
6968
70- sentry .SetTag (ctx , "linode_id" , strconv .Itoa (id ))
69+ return linodeByName (ctx , i .client , nodeName )
70+ }
7171
72- _ , err = linodeByID (ctx , i . client , id )
73- if err != nil {
72+ func ( i * instances ) InstanceExists (ctx context. Context , node * v1. Node ) ( bool , error ) {
73+ if _ , err := i . lookupLinode ( ctx , node ); err != nil {
7474 if apiError , ok := err .(* linodego.Error ); ok && apiError .Code == http .StatusNotFound {
7575 return false , nil
7676 }
77- sentry .CaptureError (ctx , err )
7877 return false , err
7978 }
8079
8180 return true , nil
8281}
8382
8483func (i * instances ) InstanceShutdown (ctx context.Context , node * v1.Node ) (bool , error ) {
85- providerID := node .Spec .ProviderID
86-
87- ctx = sentry .SetHubOnContext (ctx )
88- sentry .SetTag (ctx , "provider_id" , providerID )
89-
90- id , err := parseProviderID (providerID )
84+ instance , err := i .lookupLinode (ctx , node )
9185 if err != nil {
92- sentry .CaptureError (ctx , err )
93- return false , err
94- }
95-
96- sentry .SetTag (ctx , "linode_id" , strconv .Itoa (id ))
97-
98- instance , err := linodeByID (ctx , i .client , id )
99- if err != nil {
100- sentry .CaptureError (ctx , err )
10186 return false , err
10287 }
10388
@@ -112,29 +97,9 @@ func (i *instances) InstanceShutdown(ctx context.Context, node *v1.Node) (bool,
11297}
11398
11499func (i * instances ) InstanceMetadata (ctx context.Context , node * v1.Node ) (* cloudprovider.InstanceMetadata , error ) {
115- var (
116- providerID = node .Spec .ProviderID
117- nodeName = types .NodeName (node .Name )
118- linode * linodego.Instance
119- err error
120- )
121-
122- // TODO(okokes): abstract this away and reuse it in our other methods here
123- if providerID != "" {
124- id , err := parseProviderID (providerID )
125- if err != nil {
126- return nil , err
127- }
128-
129- linode , err = linodeByID (ctx , i .client , id )
130- if err != nil {
131- return nil , err
132- }
133- } else {
134- linode , err = linodeByName (ctx , i .client , nodeName )
135- if err != nil {
136- return nil , err
137- }
100+ linode , err := i .lookupLinode (ctx , node )
101+ if err != nil {
102+ return nil , err
138103 }
139104
140105 addresses , err := i .nodeAddresses (ctx , linode )
@@ -144,7 +109,7 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
144109
145110 // note that Zone is omitted as it's not a thing in Linode
146111 meta := & cloudprovider.InstanceMetadata {
147- ProviderID : providerID ,
112+ ProviderID : node . Spec . ProviderID , // TODO(okokes): this is circular... should we instead set it to a known prefix + linode.ID?
148113 NodeAddresses : addresses ,
149114 InstanceType : linode .Type ,
150115 Region : linode .Region ,
0 commit comments