Skip to content

Commit

Permalink
fixed logging in provider (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklypriyansh-2 authored Oct 10, 2023
1 parent 7b5248d commit d034365
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
10 changes: 7 additions & 3 deletions cmd/virtual-kubelet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func initCommandFlags() {
virtualKubeletCommand.Flags().BoolVar(&inputs.DisableTaint, "Disable taint flag", inputs.DisableTaint, "Disable the tainted effect")
virtualKubeletCommand.Flags().StringVar(&inputs.ApiKey, "api-key", inputs.ApiKey, "API key for the Salad Client")
virtualKubeletCommand.Flags().StringVar(&inputs.ProjectName, "projectName", inputs.ProjectName, "Project name for Salad Client")
virtualKubeletCommand.Flags().StringVar(&inputs.LogLevel, "logLevel", inputs.LogLevel, "Log level for the node, default to info")

markFlagRequired("organizationName")
markFlagRequired("projectName")
Expand All @@ -85,9 +86,12 @@ func markFlagRequired(flagName string) {

func runNode(ctx context.Context) error {
logrus.Infof("Running node with name prefix: %s", inputs.NodeName)
logrus.Infof("Running node with log level: %s", inputs.LogLevel)
inputs.NodeName = fmt.Sprintf("%s-%s", inputs.NodeName, randSeq(3))

node, err := nodeutil.NewNode(inputs.NodeName, newSaladCloudProvider, withClient, withTaint)
node, err := nodeutil.NewNode(inputs.NodeName, func(config nodeutil.ProviderConfig) (nodeutil.Provider, node.NodeProvider, error) {
return newSaladCloudProvider(ctx, config)
}, withClient, withTaint)
if err != nil {
logrus.WithError(err).Error("Failed to create new node")
return err
Expand All @@ -112,8 +116,8 @@ func runNode(ctx context.Context) error {
return nil
}

func newSaladCloudProvider(pc nodeutil.ProviderConfig) (nodeutil.Provider, node.NodeProvider, error) {
p, err := provider.NewSaladCloudProvider(context.Background(), inputs)
func newSaladCloudProvider(ctx context.Context, pc nodeutil.ProviderConfig) (nodeutil.Provider, node.NodeProvider, error) {
p, err := provider.NewSaladCloudProvider(ctx, inputs)
if err != nil {
logrus.WithError(err).Error("Failed to create SaladCloud provider")
return nil, nil, err
Expand Down
26 changes: 14 additions & 12 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SaladCloudProvider struct {
operatingSystem string
apiClient *saladclient.APIClient
countryCodes []saladclient.CountryCode
logger log.Logger
}

const (
Expand All @@ -45,6 +46,7 @@ func NewSaladCloudProvider(ctx context.Context, inputVars models.InputVars) (*Sa
cloudProvider := &SaladCloudProvider{
inputVars: inputVars,
apiClient: saladclient.NewAPIClient(saladclient.NewConfiguration()),
logger: log.G(ctx),
}
cloudProvider.setNodeCapacity()
cloudProvider.setCountryCodes([]string{"US"})
Expand Down Expand Up @@ -85,7 +87,7 @@ func (p *SaladCloudProvider) getNodeCapacity() corev1.ResourceList {
func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) error {
ctx, span := trace.StartSpan(ctx, "CreatePod")
defer span.End()
log.G(ctx).Debug("creating a CreatePod", pod.Name)
p.logger.Debug("creating a CreatePod", pod.Name)
createContainerObject := p.createContainersObject(pod)
createContainerGroup := p.createContainerGroup(createContainerObject, pod)

Expand All @@ -97,7 +99,7 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
createContainerGroup[0],
).Execute()
if err != nil {
log.G(ctx).Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", r)
p.logger.Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", r)
return err
}

Expand All @@ -106,7 +108,7 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err

startHttpResponse, err := p.apiClient.ContainerGroupsAPI.StartContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(pod.Namespace, pod.Name, nil)).Execute()
if err != nil {
log.G(ctx).Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", startHttpResponse)
p.logger.Errorf("Error when calling `ContainerGroupsAPI.CreateContainerGroupModel`", startHttpResponse)
err := p.DeletePod(ctx, pod)
if err != nil {
return err
Expand Down Expand Up @@ -143,7 +145,7 @@ func (p *SaladCloudProvider) CreatePod(ctx context.Context, pod *corev1.Pod) err
})
}

log.G(ctx).Infof("Done creating the container and initiating the startup ", pod)
p.logger.Infof("Done creating the container and initiating the startup ", pod)
return nil
}

Expand All @@ -152,14 +154,14 @@ func (p *SaladCloudProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) err
}

func (p *SaladCloudProvider) DeletePod(ctx context.Context, pod *corev1.Pod) error {
ctx, span := trace.StartSpan(ctx, "DeletePod")
_, span := trace.StartSpan(ctx, "DeletePod")
defer span.End()
log.G(ctx).Debug("deleting a pod")
p.logger.Debug("deleting a pod")
response, err := p.apiClient.ContainerGroupsAPI.DeleteContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(pod.Namespace, pod.Name, pod)).Execute()
pod.Status.Phase = corev1.PodSucceeded
pod.Status.Reason = "Pod Deleted"
if err != nil {
log.G(ctx).Errorf("Error when deleting the container ", response)
p.logger.Errorf("Error when deleting the container ", response)
return err
}
now := metav1.Now()
Expand All @@ -173,15 +175,15 @@ func (p *SaladCloudProvider) DeletePod(ctx context.Context, pod *corev1.Pod) err
},
}
}
log.G(ctx).Infof("Done deleting the container ", pod)
p.logger.Infof("Done deleting the container ", pod)
return nil
}

func (p *SaladCloudProvider) GetPod(ctx context.Context, namespace string, name string) (*corev1.Pod, error) {

resp, r, err := saladclient.NewAPIClient(saladclient.NewConfiguration()).ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(namespace, name, nil)).Execute()
if err != nil {
log.G(ctx).Errorf("Error when calling `ContainerGroupsAPI.GetPod`", r)
p.logger.Errorf("Error when calling `ContainerGroupsAPI.GetPod`", r)
return nil, err
}
startTime := metav1.NewTime(resp.CreateTime)
Expand Down Expand Up @@ -222,12 +224,12 @@ func (p *SaladCloudProvider) contextWithAuth() context.Context {
}

func (p *SaladCloudProvider) GetPodStatus(ctx context.Context, namespace string, name string) (*corev1.PodStatus, error) {
ctx, span := trace.StartSpan(ctx, "GetPodStatus")
_, span := trace.StartSpan(ctx, "GetPodStatus")
defer span.End()

containerGroup, response, err := p.apiClient.ContainerGroupsAPI.GetContainerGroup(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName, utils.GetPodName(namespace, name, nil)).Execute()
if err != nil {
log.G(ctx).Errorf("ContainerGroupsAPI.GetPodStatus ", response)
p.logger.Errorf("ContainerGroupsAPI.GetPodStatus ", response)
return nil, err
}

Expand All @@ -250,7 +252,7 @@ func (p *SaladCloudProvider) GetPods(ctx context.Context) ([]*corev1.Pod, error)

resp, r, err := p.apiClient.ContainerGroupsAPI.ListContainerGroups(p.contextWithAuth(), p.inputVars.OrganizationName, p.inputVars.ProjectName).Execute()
if err != nil {
log.G(ctx).Errorf("Error when list ContainerGroupsAPI.ListContainerGroups ", r)
p.logger.Errorf("Error when list ContainerGroupsAPI.ListContainerGroups ", r)
return nil, err
}
pods := make([]*corev1.Pod, 0)
Expand Down

0 comments on commit d034365

Please sign in to comment.