Skip to content

Commit 939e6cf

Browse files
author
Priya Wadhwa
committed
Delete and redeploy object upon error 'field is immutable'
As discussed in #891, when running skaffold dev certain immutable Kubernetes objects (like Jobs) can't be redeployed. A 'field is immutable' error is returned when this happens. To fix this issue, we can check the error from kubectl apply for 'field is immutable'. If we find it, we can delete the object and try to deploy it again.
1 parent cea1e93 commit 939e6cf

File tree

1 file changed

+22
-2
lines changed
  • pkg/skaffold/deploy/kubectl

1 file changed

+22
-2
lines changed

pkg/skaffold/deploy/kubectl/cli.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package kubectl
1818

1919
import (
20+
"bufio"
21+
"bytes"
2022
"context"
2123
"io"
2224
"os/exec"
25+
"strings"
2326

2427
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2528
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
@@ -56,8 +59,25 @@ func (c *CLI) Apply(ctx context.Context, out io.Writer, manifests ManifestList)
5659
return nil, nil
5760
}
5861

59-
if err := c.Run(ctx, manifests.Reader(), out, "apply", c.Flags.Apply, "-f", "-"); err != nil {
60-
return nil, errors.Wrap(err, "kubectl apply")
62+
buf := bytes.NewBuffer([]byte{})
63+
writer := bufio.NewWriter(buf)
64+
if err := c.Run(ctx, manifests.Reader(), writer, "apply", c.Flags.Apply, "-f", "-"); err != nil {
65+
if !strings.Contains(buf.String(), "field is immutable") {
66+
return nil, err
67+
}
68+
// If the output contains the string 'field is immutable', we want to delete the object and recreate it
69+
// See Issue #891 for more information
70+
if err := c.Detete(ctx, out, manifests); err != nil {
71+
return nil, errors.Wrap(err, "deleting manifest")
72+
}
73+
if err := c.Run(ctx, manifests.Reader(), out, "apply", c.Flags.Apply, "-f", "-"); err != nil {
74+
return nil, errors.Wrap(err, "kubectl apply after deletion")
75+
}
76+
} else {
77+
// Write output to out
78+
if _, err := out.Write(buf.Bytes()); err != nil {
79+
return nil, errors.Wrap(err, "writing to out")
80+
}
6181
}
6282

6383
return updated, nil

0 commit comments

Comments
 (0)