Repository: k3s-io/k3s
File: pkg/cluster/storage.go (Go)

Code before:

		return true, c.ReconcileBootstrapData(ctx, bytes.NewReader(data), &c.config.Runtime.ControlRuntimeBootstrap, false)
	})
}

// getBootstrapData makes a single attempt to retrieve and decrypt bootstrap data from the datastore.
func (c *Cluster) getBootstrapData(ctx context.Context, token string) ([]byte, error) {
	storageClient, err := client.New(c.config.Runtime.EtcdConfig)
	if err != nil {
		return nil, err
	}
	defer storageClient.Close()

	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
	defer cancel()

	value, err := storageClient.Get(ctx, storageKey(token))
	if err != nil {
		return nil, err
	}

	return decrypt(token, value.Data)
}

// validateDatastoreConnection checks if the datastore is reachable and can list keys under "/bootstrap".
func validateDatastoreConnection(ctx context.Context, storageClient client.Client) ([]client.Value, error) {
	var bootstrapList []client.Value
	var err error

	if err := wait.PollUntilContextCancel(ctx, 5*time.Second, true, func(ctx context.Context) (bool, error) {
		operationCtx, operationCancel := context.WithTimeout(ctx, 10*time.Second)
		defer operationCancel()
	
		bootstrapList, err = storageClient.List(operationCtx, "/bootstrap", 0)
		if err != nil {
			if errors.Is(err, rpctypes.ErrGPRCNotSupportedForLearner) {
				return false, nil
			}
			return false, err
		}
		return true, nil
	}); err != nil {
		logrus.Errorf("Failed to validate datastore connection: %v", err)
		return nil, err
	}
	return bootstrapList, nil
}

// getBootstrapKeyFromStorage will list all keys that has prefix /bootstrap and will check for key that is
// hashed with empty string and will check for any key that is hashed by different token than the one
// passed to it, it will return error if it finds a key that is hashed with different token and will return

Review comment (suggestion):
```suggestion
// getBootstrapValues returns the value of all keys under the "/bootstrap" prefix, with a 10 second timeout. 
func getBootstrapValues(ctx context.Context, storageClient client.Client) ([]client.Value, error) {
```

Code after:

		return true, c.ReconcileBootstrapData(ctx, bytes.NewReader(data), &c.config.Runtime.ControlRuntimeBootstrap, false)
	})
}

// getBootstrapData makes a single attempt to retrieve and decrypt bootstrap data from the datastore.
func (c *Cluster) getBootstrapData(ctx context.Context, token string) ([]byte, error) {
	storageClient, err := client.New(c.config.Runtime.EtcdConfig)
	if err != nil {
		return nil, err
	}
	defer storageClient.Close()

	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
	defer cancel()

	value, err := storageClient.Get(ctx, storageKey(token))
	if err != nil {
		return nil, err
	}

	return decrypt(token, value.Data)
}

// getBootstrapValues returns the value of all keys under the "/bootstrap" prefix, with a 10 second timeout. 
func getBootstrapValues(ctx context.Context, storageClient client.Client) ([]client.Value, error) {
	var bootstrapList []client.Value
	var err error

	if err := wait.PollUntilContextCancel(ctx, 5*time.Second, true, func(ctx context.Context) (bool, error) {
		operationCtx, operationCancel := context.WithTimeout(ctx, 10*time.Second)
		defer operationCancel()
	
		bootstrapList, err = storageClient.List(operationCtx, "/bootstrap", 0)
		if err != nil {
			if errors.Is(err, rpctypes.ErrGPRCNotSupportedForLearner) {
				return false, nil
			}
			return false, err
		}
		return true, nil
	}); err != nil {
		logrus.Errorf("Failed to validate datastore connection: %v", err)
		return nil, err
	}
	return bootstrapList, nil
}

// getBootstrapKeyFromStorage will list all keys that has prefix /bootstrap and will check for key that is
// hashed with empty string and will check for any key that is hashed by different token than the one
// passed to it, it will return error if it finds a key that is hashed with different token and will return