kubernetes/kops · error

AsBytes called on nil Certificate

Error message

AsBytes called on nil Certificate

What it means

Same nil-receiver guard as AsString but for Certificate.AsBytes: when a template renders a certificate as bytes and the Certificate pointer is nil, this sentinel fires. It indicates the referenced certificate was not issued/loaded before rendering, and prevents a panic inside template execution.

Source

Thrown at pkg/pki/certificate.go:130

func (c *Certificate) AsString() (string, error) {
	// Nicer behaviour because this is called from templates
	if c == nil {
		return "", fmt.Errorf("AsString called on nil Certificate")
	}

	var data bytes.Buffer
	_, err := c.WriteTo(&data)
	if err != nil {
		return "", fmt.Errorf("error writing SSL certificate: %v", err)
	}
	return data.String(), nil
}

func (c *Certificate) AsBytes() ([]byte, error) {
	// Nicer behaviour because this is called from templates
	if c == nil {
		return nil, fmt.Errorf("AsBytes called on nil Certificate")
	}

	var data bytes.Buffer
	_, err := c.WriteTo(&data)
	if err != nil {
		return nil, fmt.Errorf("error writing SSL certificate: %v", err)
	}
	return data.Bytes(), nil
}

func (c *Certificate) WriteTo(w io.Writer) (int64, error) {
	// For the dry-run case
	if c.Certificate == nil {
		return 0, nil
	}

	var b bytes.Buffer
	err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: c.Certificate.Raw})

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Ensure the certificate is populated before code/templates call AsBytes
  2. Trace why the Certificate pointer is nil (load or issuance failure)
  3. Handle missing certificates explicitly in template rendering
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pkg/pki/certificate.go:130 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/11525f3b3238d7cf. Report an issue: GitHub.